Mybatis:Mybatis注解实现动态SQL注意事项
1. 使用<script>声明动态SQL
@Select("<script>" +
"select * from User " +
"<where>" +
" id = #{id}" +
"</where>" +
"</script>")
User selectUser(int id);
2. 判断空字符用单引号
@Select("<script>" +
"select * from User " +
"<where>" +
"<if test=\"name != null and name != ''\">" +
" name = #{name}" +
"</if>" +
"</where>" +
"</script>")
User selectUser(String name);
3. if、when等判断条件拼接用and或or
@Select("<script>" +
"select * from User " +
"<where>" +
"<if test=\"name != null and name != ''\">" +
" name = #{name}" +
"</if>" +
"</where>" +
"</script>")
User selectUser(String name);
4. 模糊查询使用concat()拼接函数
@Select("<script>" +
"select * from User " +
"<where>" +
"<if test=\"name != null and name != ''\">" +
" name like concat('%', #{name}, '%') " +
"</if>" +
"</where>" +
"</script>")
User selectUser(String name);
-----------------------------------
作者:怒吼的萝卜
链接:http://www.cnblogs.com/nhdlb/
-----------------------------------