在搜索时通常需要实现模糊查询
最开始我是这样写的:
@Select({"<script>",
"SELECT * FROM book",
"WHERE 1=1",
"<when test='name!=null'>",
"AND NAME = %#{name}%",
"</when>",
"<when test='type!=null'>",
"AND TYPE = #{type}",
"</when>",
"<when test='description!=null'>",
"AND description = %#{description}%",
"</when>",
"ORDER BY create_time desc",
"</script>"})
List<Book> findBooksByCondition(Book book);
并不行
后来使用like(太久不用,忘了)
@Select({"<script>",
"SELECT * FROM book",
"WHERE 1=1",
"<when test='name!=null'>",
"AND NAME like #{name}",
"</when>",
"<when test='type!=null'>",
"AND TYPE = #{type}",
"</when>",
"<when test='description!=null'>",
"AND description like #{description}",
"</when>",
"ORDER BY create_time desc",
"</script>"})
List<Book> findBooksByCondition(Book book);
还是没办法做到模糊查询
最终做法:
@Select({"<script>",
"SELECT * FROM book",
"WHERE 1=1",
"<when test='name!=null'>",
"AND NAME like CONCAT('%',#{name},'%')",
"</when>",
"<when test='type!=null'>",
"AND TYPE = #{type}",
"</when>",
"<when test='description!=null'>",
"AND description like CONCAT('%',#{description},'%')",
"</when>",
"ORDER BY create_time desc",
"</script>"})
List<Book> findBooksByCondition(Book book);
这样就可以了
使用CONCAT('%',#{name},'%')
拼接字符串作为查询条件
转载请注明:汪明鑫的个人博客 » mybatis基于注解的方式实现模糊查询
说点什么
您将是第一位评论人!