需求:实现多条件查询用户(姓名模糊匹配, 年龄在指定的最小值到最大值之间)
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; //...}
测试
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);Listlist = session.selectList(statement, parameter);System.out.println(list);session.close();
MyBatis中可用的动态SQL标签
if
choose (when otherwise)
trim (where set)
foreach