测试代码区:
ConditionUser 类的{
private String name;
private int minAge; private int maxAge;}
SqlSession session=MyBatisUtils.getSession();
String statement="com.day03_mybaits.test7.userMapper.getUser"; String name="o"; //如果name=null的处理办法,在xml中 name=null; ConditionUser prameter=new ConditionUser("%"+name+"%", 13, 18); //Classes classes=session.selectOne(statement,prameter); List<User>list=session.selectList(statement, prameter); System.out.println(list);配置文件中的实现:
<!-- 实现多条件查询用户(姓名模糊匹配,年龄在指定的最小值和最大值之间) -->
<select id="getUser" parameterType="_ConditionUser" resultType="_User" > select * from d_user where <if test='name!="%null%"'> <!--必须加上and要么会报错 --> name like #{name} and </if> age between #{minAge} and #{maxAge} </select>