mybatis的动态sql之if test的使用说明
<select parameterType='java.lang.String' resultType='java.lang.String'> SELECT MAX(DEPART_ID) FROM T_P_DEPART <where> <if test='_parameter!=null and _parameter!=’’'> AND DEPART_PID = #{departId,jdbcType=VARCHAR} </if> <if test='_parameter==null or _parameter==’’'> AND DEPART_PID IS NULL </if> </where> </select>参数为pojo , if test读取该参数代码
<select parameterType='ShopVo' resultType='ShopCustomer'> select * from shop <where> <if test='shopCustomer.shopname!=null and shopCustomer.shopname!=’’'>shop.shopname like ’%${shopCustomer.shopname}%’ </if> <if test='shopCustomer.shopname==null or shopCustomer.shopname==’’'> AND shop.shopname is null </if> </where></select>
补充:关于mybatis中 if test的条件怎么写
1.mybatis 中 的 if test写法1.1官方文档上对于if是这么写的<if test='title != null'> AND title like #{title}</if>
参考官方文档:
实际项目中会有这种情况: 页面上title字段输入某个值进行查询,手动将输入框中的值删除,然后再次查询,发现结果不正确,究其原因是应为title传入了空串' ' 这样在mybatis配置文件中就会用空串进行查询,导致出现错误结果
1.2建议写法<if test='title != null and title != ’’' > AND title like #{title}</if>2.使用mybatis 做修改时将字段置空
if中如果传入的参数如果为空,那么将不会执行if中的语句
解决办法:<update parameterType='*.*.Object' >update table <set> <if test='Object.fullName == null or Object.fullName ==’’'> full_name = null, </if> <if test='Object.fullName != null and Object.fullName !=’’'> full_name = #{companyOrg.fullName}, </if> <if test='Object.level == null or Object.level ==’’'> level = null, </if> <if test='Object.level == 0 '> level = null, </if> <if test='Object.level != null and Object.level !=’’ and Object.level != 0 '> level = #{companyOrg.level}, </if> </set> where 1=1 and id =#{companyOrg.id}</update>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。
相关文章:
1. Navicat连接Oracle数据库的详细步骤与注意事项2. 一文教会你配置使用Navicat或PLSQL可视化工具远程连接Oracle3. 浅谈mybatis 乐观锁实现,解决并发问题4. MySQL找出未提交事务的SQL实例浅析5. 如何利用MySQL查询varbinary中存储的数据6. SQL Server中索引使用及维护7. mybatis 如何判断list集合是否包含指定数据8. SQL Server跨服务器操作数据库的图文方法(LinkedServer)9. MySQL Flink Watermark实现事件时间处理的关键技术10. Mysql命令行连接远程/本地数据库详解
