码迷,mamicode.com
首页 > 数据库 > 详细

8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

时间:2016-02-24 22:48:59      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:

多参数查询,使用parameterType。实例:

 

用户User[id, name, age]

1.mysql建表并插入数据

技术分享

2.Java实体类

public class User {
    
    public User() {
    }
    public User(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
}

3.创建查询参数实体类

/**
 * 模糊查询User的类
 * @author Administrator
 *
 */
public class SelectUserCondition {
    
    private String name;//姓名
    private int minAge;//最小年龄
    private int maxAge;//最大年龄
    
    
    public SelectUserCondition() {
        super();
    }
    public SelectUserCondition(String name, int minAge, int maxAge) {
        super();
        this.name = name;
        this.minAge = minAge;
        this.maxAge = maxAge;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMinAge() {
        return minAge;
    }
    public void setMinAge(int minAge) {
        this.minAge = minAge;
    }
    public int getMaxAge() {
        return maxAge;
    }
    public void setMaxAge(int maxAge) {
        this.maxAge = maxAge;
    }
    
}

4.sql查询的配置文件selectUserMapper.xml

<mapper namespace="com.mlxs.mybatis.test7.selectUserMapper">
    <!-- 
        动态sql,模糊查询  parameterType为查询条件实体
  -->
  <select id="getConditionUser" parameterType="SelectUserCondition" resultType="User">
    SELECT * FROM users WHERE
<if test=‘name != "%null%"‘> NAME LIKE #{name} AND </if> age BETWEEN #{minAge} AND #{maxAge}
  </select>

</mapper>

5.测试类:

/**
 * 动态sql模糊查询
 * 
 * 注意:当name=null时,"%"+name+"%"=%null%
 * @author 魅力_小生
 *
 */
public class _Test7SelectConditionUser {
    
    @Test
    public void getConditionUser(){
        //创建session,设置事务为true
        SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
        String statement = "com.mlxs.mybatis.test7.selectUserMapper.getConditionUser";
        //定义查询条件实体
        SelectUserCondition condition = new SelectUserCondition("%a%", 20, 100);
        List<User> ulist = session.selectList(statement, condition);
        System.out.println("userList--->"+ulist);
        session.close();
    }
}

6.结果:

userList--->[User [id=3, name=update2, age=100], User [id=5, name=add1, age=23]]

8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

标签:

原文地址:http://www.cnblogs.com/yzlpersonal/p/5215348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!