java mybatis sql 判断是否某数据存在重名

背景

今天看业务代码,自己写接口实现判断重名时,遇到一个小问题

判断某项数据是否在数据库中存在重名。

代码

判断时,判断数据库中有没有和该数据name相同但是id不同的数据即可。

count(1)是计算符合查询条件的数据行数有多少。(和count(*)相同)

CaseManageMapper.java

...
<select id = "isSameName" resultType="java.lang.Integer">
	select count(1) from t_case
	<where>
		1 = 1
		<if test="name != null and name != ''">
			and name = #{name}
		</if>
		<if test="id != null and id != ''">
			and id != #{id}
		</if>
	</where>
</select>
posted @ 2024-06-25 16:18  r涤生  阅读(8)  评论(0编辑  收藏  举报