zoukankan      html  css  js  c++  java
  • 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

    多参数查询,使用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]]

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    python3中模块初识
    Django 应用程序 + 模型 + 基本数据访问
    Axure文本框验证和外部url的调用
    MATLAB 实时脚本(live-script)使用
    Django MTV 开发模式 + 数据库配置
    Django 模板继承
    Django 修改视图文件(views.py)并加载Django模块 + 利用render_to_response()简化加载模块 +locals()
    Django Context对象 + 过滤器 + 标签
    Axure 页面内多组内容切换的实现 + 利用一个内联框架实现百度地图访问
    MATLAB绘图功能(2) 二维底层绘图修饰
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/5215348.html
Copyright © 2011-2022 走看看