Mybatis中的like查询

时间:2024-05-12 08:35:56

今天要做一个模糊查询

用的Mybatis

开始写的是:

  1. select id,bookName,author,publisher,donor,status,createDate,lastUpdate from book
  2. <where>
  3. <if test="bookName!=null">
  4. bookName like '%#{bookName}%'
  5. </if>
  6. <if test="author!=null">
  7. and author like '%#{author}%'
  8. </if>

最后改为:

  1. select id,bookName,author,publisher,donor,status,createDate,lastUpdate from book
  2. <where>
  3. <if test="bookName!=null">
  4. bookName like CONCAT('%','${bookName}','%' )
  5. </if>
  6. <if test="author!=null">
  7. and author like CONCAT('%','${author}','%' )
  8. </if>

主要还是MyBatis传值的问题啊

如果不是字符串就没法替换了

详细源码更多技术支持来源查询:minglisoft.cn/technology