使用TableLogic注解碰到的问题随笔

时间:2025-03-18 07:27:50
使用TableLogic注解碰到的问题

使用QueryWrapper作为查询条件时,拼接的sql语句自动包含:where del_flag = 0。但是我希望查询结果包含删除记录。为了避免影响其它接口,就没有去操作实体类上的@TableLogic注解。后面的解决方式是:不使用QueryWrapper,自己写文件了。

动态sql中包含and、or等的写法

参数类型是User类型。逻辑就是如果参数User中的id存在,查询除当前id以外的其它记录,是否有和参数User中名称、邮箱、电话任意一个属性相同的记录。如果id不存在,就查询全表咯。

SELECT * FROM user where 1=1
			<if test="id != null and id != ''">
				and id != #{id}
			</if>
		and (
			<trim prefixOverrides="or">
				<if test="username != null and username != ''">
					username = #{username}
				</if>
				<if test="email != null and email != ''">
					or email = #{email}
				</if>
				<if test="phone != null and phone != ''">
					or phone = #{phone}
				</if>
			</trim>
		)
		limit 1;

where 1=1 还是好用啊,trim标签也好用。