MyBatisPlus 之 条件构造器 EntityWrapper

一、EntityWrapper 简介

  1、MyBatis-Plus 通过 EntityWrapper(简称 EW,MP封装的一个查询条件构造器)或者 Condition(与EW类似)来让用户自由的构建查询条件,简单便捷,没有额外的负担,能够有效提高开发效率。

  2、实体包装器,主要用于处理 SQL拼接,排序,实体参数查询等;

  3、注意:使用的是数据库字段,不是 Java 属性;

  4、条件参数说明

二、带条件的查询

  案例1:

    /**
     * 条件构造器  查询操作
     * 分页查询tbl_employee表中, 年龄在 18-50 之间性别为男且姓名为 Tom 的所有用户
     * SQL 语句:
     *  Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee
     *   WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)
     *
     *   Parameters: 18(Integer), 50(Integer), 1(Integer), Tom(String)
     */
    @Test
    public void testEntityWrapperSelect() {
        Wrapper<Employee> entityWrapper = new EntityWrapper<>();
        entityWrapper.between("age", 18, 50)
                .eq("gender", 1)
                .eq("last_name", "Tom");

        List<Employee> empList = employeeMapper.selectPage(new Page<>(1, 2), entityWrapper);

        empList.forEach(System.out::println);
    }

    BaseMapper 接口中的方法说明:

    /**
     * <p>
     * 根据 entity 条件,查询全部记录(并翻页)
     * </p>
     *
     * @param rowBounds 分页查询条件(可以为 RowBounds.DEFAULT)
     * @param wrapper   实体对象封装操作类(可以为 null)
     * @return List<T>
     */
    List<T> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);

  案例2:

    /**
     * 查询 tbl_employee 表中,性别为女且名字中带有 "老师" 或者 邮箱中带有 "a"
     *
     *  SQL 语句:
     *
     *  使用 or() 方法
     *  Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee
     *  WHERE (gender = ? AND last_name LIKE ? OR email LIKE ?)
     *  Parameters: 0(Integer), %老师%(String), %a%(String)
     *
     *
     *  使用 orNew() 方法
     *  Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee
     *  WHERE (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
     *  Parameters: 0(Integer), %老师%(String), %a%(String)
     *
     * 可以看到 or() 与 orNew() 方法的区别就在于是否会用括号括起来新的条件。
     */
    @Test
    public void testEntityWrapperSelectList() {
        Wrapper<Employee> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("gender", 0)
                .like("last_name", "老师")
                //.or()
                .orNew()
                .like("email", "a");

        List<Employee> empList = employeeMapper.selectList(entityWrapper);

        empList.forEach(System.out::println);
    }

    方法说明:

    /**
     * <p>
     * 根据 entity 条件,查询全部记录
     * </p>
     *
     * @param wrapper 实体对象封装操作类(可以为 null)
     * @return List<T>
     */
    List<T> selectList(@Param("ew") Wrapper<T> wrapper);

  

三、带条件的修改

  案例1:

    /**
     * 根据条件更新信息
     *
     * SQL 语句:
     *  Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE (last_name = ? AND age = ?)
     *  Parameters: Tony老师(String), Tony@qq.com(String), 1(Integer), Smith(String), 13(Integer)
     */
    @Test
    public void testEntityWrapperUpdate() {
        Wrapper<Employee> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("last_name", "Smith")
                .eq("age", 13);

        Employee emp = new Employee();
        emp.setLastName("Tony老师");
        emp.setEmail("Tony@qq.com");
        emp.setGender(1);

        Integer result = employeeMapper.update(emp, entityWrapper);
        System.out.println("result = " + result);

    }

    接口中方法声明:

    /**
     * <p>
     * 根据 whereEntity 条件,更新记录
     * </p>
     *
     * @param entity  实体对象
     * @param wrapper 实体对象封装操作类(可以为 null)
     * @return
     */
    Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> wrapper);

四、带条件的删除

  案例1:

    /**
     * 根据条件删除
     * SQL 语句;
     *  Preparing: DELETE FROM tbl_employee WHERE (last_name = ? AND age = ?)
     *  Parameters: Enma(String), 45(Integer)
     */
    @Test
    public void testEntityWrapperDelete() {
        Wrapper<Employee> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("last_name", "Enma")
                    .eq("age", 45);

        Integer result = employeeMapper.delete(entityWrapper);
        System.out.println("result = " + result);
    }

    接口中方法的说明:

    /**
     * <p>
     * 根据 entity 条件,删除记录
     * </p>
     *
     * @param wrapper 实体对象封装操作类(可以为 null)
     * @return int
     */
    Integer delete(@Param("ew") Wrapper<T> wrapper);

五、EntityWrapper 常用方法

  案例:

    /**
     * 查询性别为女的,根据 age 进行排序(asc/desc),简单分页
     */
    @Test
    public void testEntityWrapperOther() {
        Wrapper<Employee> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("gender", 1)
                .orderBy("age") //默认升序
                //.orderDesc(Arrays.asList("age"));
                .last("desc limit 1,2");  //手动把sql拼接到最后(有sql注入的风险,请谨慎使用)

        List<Employee> empList = employeeMapper.selectList(entityWrapper);
        empList.forEach(System.out::println);
    }

  除了支持排序,分页,等于等条件,更多条件请参照估官方文档。

  官方文档:EntityWrapper

六、使用Condition的方式

  案例:

    /**
     * 查询操作
     * 分页查询tbl_employee表中, 年龄在 18-50 之间性别为男且姓名为 Tom 的所有用户
     * SQL 语句:
     */
    @Test
    public void testCondition() {
        Condition condition = Condition.create();
        condition.between("age", "18", "50")
                .eq("gender", "1")
                .eq("last_name", "Tom");

        List empList = employeeMapper.selectPage(new Page<Employee>(1, 2), condition);
        empList.forEach(System.out::println);
    }

  

  Condition 类说明:

    

    Condition 是 Wrapper 的子类,所以也可以来构造条件表达式。

七、小结

  MP:EntityWrapper  Condition 条件构造器。

  类似于 xxxExample 中的 Criteria 的 QBC(Query By Criteria)。

  

 
原文地址:https://www.cnblogs.com/niujifei/p/15333042.html