Mybatis学习笔记-动态SQL与模糊查询
2014-09-29 11:15
543 查看
需求:实现多条件查询用户(姓名模糊匹配, 年龄在指定的最小值到最大值之间)
User.java实体类
ConditionUser.java
MyBatis中可用的动态SQL标签
if
choose (when otherwise)
trim (where set)
foreach
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1559302
User.java实体类
public class User { private int id; private String name; private int age; //... }
ConditionUser.java
public class ConditionUser { private String name; private int minAge; private int maxAge; //... }
<!-- 实现多条件查询用户(姓名模糊匹配, 年龄在指定的最小值到最大值之间) 类似jstl表达式 --> <select id="getUser" parameterType="com.mybatis.test05.ConditionUser" resultType="com.mybatis.test05.User"> select * from d_user where <if test='name != "%null%"'> name like #{name} and </if> age between #{minAge} and #{maxAge} </select>测试
SqlSessionFactory factory = MybatisUtil.getFactory(); SqlSession session = factory.openSession(); String statement = "com.mybatis.test05.userMapper.getUser"; String name = "o"; name = null; ConditionUser parameter = new ConditionUser("%"+name+"%", 13, 18); List<User> list = session.selectList(statement, parameter); System.out.println(list); session.close();
MyBatis中可用的动态SQL标签
if
choose (when otherwise)
trim (where set)
foreach
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1559302
相关文章推荐
- Mybatis学习笔记-动态SQL与模糊查询
- mybatis学习笔记3 - 动态sql
- MyBatis:学习笔记(4)——动态SQL
- mybatis学习笔记(8)-动态sql
- Mybatis学习笔记(九)【动态sql】
- J2EE系列之MyBatis学习笔记(六)-- 动态sql
- Mybatis 学习笔记 动态sql语句
- Mybatis学习笔记——动态Sql
- MyBatis学习笔记-07.MyBatis动态Sql语句if、where、trim、set
- Mybatis学习笔记-动态sql
- myBatis学习笔记(6)——动态SQL & 模糊查询
- Mybatis学习笔记-动态SQL和模糊查询
- Mybatis学习笔记--(二)动态SQL
- mybatis学习笔记(8)-动态sql
- MyBatis学习笔记(六)动态sql
- mybatis学习笔记(2)--动态sql
- Mybatis学习笔记——动态Sql
- Java学习笔记33:mybatis动态SQL语句
- Mybatis学习笔记-动态sql
- Mybatis学习笔记之四:动态SQL