四、条件构造器——AbstractWrapper

上一节我们完成了基于mybatis-plus的CRUD操作,这一节我们来学习一下使用mybatis-plus中的条件构造器——AbstractWrapper,我们主要使用的是QueryWrapper来演示,其他的大家自己可以尝试。

首先我们来介绍一下AbstractWrapper,下图是AbstractWrapper的一个继承结构:

mp03-01.png
  1. Mybatis-Plus 通过 QueryWrapper( MP 封装的一个查询条件构造器,继承自AbstractWrapperAbstractWrapper 实现了 Wrapper等接口) 来让用户自由的构建查询条件,简单便捷,没有额外的负担,能够有效提高开发效率
  2. 查询包装器QueryWrapper, 主要用于处理 sql 拼接,排序,实体参数查询等
  3. 注意: 使用的是数据库字段,不是 Java 属性!
  4. 条件参数说明:
查询方式 说明
or 或条件语句
and 且条件语句
like 模糊查询 like
notLike 模糊查询 not Like
exists exists 条件语句
notExists not Exists 条件语句
isNull null 值查询
isNotNull is Not Null 查询
in in 查询
notIn not in 查询
groupBy 分组查询
orderBy 排序查询
having 分组后筛选
eq 等于 =
ne 不等于 <>
between between 条件语句
··· ···

首先按照快速开始——Spring集成Mybatis-Plus一节的操作,新建一个mp03Module,可以将mp02中的内容全部复制过来,删除TestMp.class的内容,以便我们使用条件构造器,在此之前我们先修改一下修改mp03的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mybatis-plus-in-action</artifactId>
        <groupId>com.demo.mybatis-plus</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mp03</artifactId>

    <dependencies>
        <!-- mp 依赖
            mybatis-plus 会自动维护mybatis 以及 mybatis-spring相关的依赖
            Mybatis 及 Mybatis-Spring 依赖请勿加入项目配置,以免引起版本冲突!!!Mybatis-Plus 会自动帮你维护!
         -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>
        <!--junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

下面开始我们的QueryWrapper的演示:

1、使用QueryWrapper的更新操作

/**
 * 条件构造器 更新操作
 */
@Test
public void testWrapperUpdate() {

    Employee employee = new Employee();
    employee.setLastName("XP");
    employee.setEmail("xp@github.com");
    employee.setGender(0);
    employeeMapper.update(employee,
            new QueryWrapper<Employee>()
                    .eq("age", 22)
                    .eq("last_name", "MP")
    );
}

2、使用QueryWrapper的查询操作

/**
 * 条件构造器 查询操作
 */
@Test
public void testWrapperSelect() {
    // 分页查询 tbl_employee 表中,年龄在 18~50 之间性别为男且
    // 姓名为 xx 的所有用户
    IPage<Employee> page = employeeMapper.selectPage(new Page<Employee>(1, 3),
            new QueryWrapper<Employee>()
                    .between("age", 18, 50)
                    .eq("gender", 1)
                    .eq("last_name", "MP")
    );
    System.out.println(page.getRecords());

    // 查询 tbl_employee 表中,名字中带有M 性别为女 或者邮箱中带有a的
    List<Employee> employees = employeeMapper.selectList(
            new QueryWrapper<Employee>()
                    .eq("gender", 0)
                    .like("last_name", "M")
                    .or() // SQL:(gender = ? AND last_name LIKE ? OR email LIKE ?)
                    .like("email", "a")
    );
    System.out.println(employees);

    // 带排序的查询
    List<Employee> list = employeeMapper.selectList(
            new QueryWrapper<Employee>()
                    .eq("gender", 1)
//                        .orderBy(true, true, "age")
                    .orderByDesc("age")
    );

    System.out.println(list);
}

3、使用QueryWrapper的删除操作

/**
 * 条件构造器 删除操作
 */
@Test
public void testWrapperDelete() {
    employeeMapper.delete(
            new QueryWrapper<Employee>()
                    .eq("age", 22)
                    .eq("last_name", "MP")
    );
}

4、完整的测试代码

public class TestMp {
    private ApplicationContext ioc = new
            ClassPathXmlApplicationContext("applicationContext.xml");

    private EmployeeMapper employeeMapper = ioc.getBean("employeeMapper", EmployeeMapper.class);

    /**
     * 条件构造器 删除操作
     */
    @Test
    public void testWrapperDelete() {
        employeeMapper.delete(
                new QueryWrapper<Employee>()
                        .eq("age", 22)
                        .eq("last_name", "MP")
        );
    }

    /**
     * 条件构造器 更新操作
     */
    @Test
    public void testWrapperUpdate() {

        Employee employee = new Employee();
        employee.setLastName("XP");
        employee.setEmail("xp@github.com");
        employee.setGender(0);
        employeeMapper.update(employee,
                new QueryWrapper<Employee>()
                        .eq("age", 22)
                        .eq("last_name", "MP")
        );
    }

    /**
     * 条件构造器 查询操作
     */
    @Test
    public void testWrapperSelect() {
        // 分页查询 tbl_employee 表中,年龄在 18~50 之间性别为男且
        // 姓名为 xx 的所有用户
        IPage<Employee> page = employeeMapper.selectPage(new Page<Employee>(1, 3),
                new QueryWrapper<Employee>()
                        .between("age", 18, 50)
                        .eq("gender", 1)
                        .eq("last_name", "MP")
        );
        System.out.println(page.getRecords());

        // 查询 tbl_employee 表中,名字中带有M 性别为女 或者邮箱中带有a的
        List<Employee> employees = employeeMapper.selectList(
                new QueryWrapper<Employee>()
                        .eq("gender", 0)
                        .like("last_name", "M")
                        .or() // SQL:(gender = ? AND last_name LIKE ? OR email LIKE ?)
                        .like("email", "a")
        );
        System.out.println(employees);

        // 带排序的查询
        List<Employee> list = employeeMapper.selectList(
                new QueryWrapper<Employee>()
                        .eq("gender", 1)
//                        .orderBy(true, true, "age")
                        .orderByDesc("age")
        );

        System.out.println(list);
    }

}

完成上面的操作后,mp03的代码结构如下所示:

mp03-02.png

至此,基于 mybatis-plus 的条件构造器——QueryWrapper演示就完成了,下面我们就可以进入到下一节ActiveRecord(活动记录)了。

源代码

相关示例完整代码:mybatis-plus-in-action

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,726评论 0 4
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,891评论 2 9
  • 本章将会介绍 存储属性的初始赋值自定义构造过程默认构造器值类型的构造器代理类的继承和构造过程可失败构造器必要构造器...
    寒桥阅读 785评论 0 0
  • 构造过程是使用类、结构体或枚举类型的实例之前的准备过程。在新实例可用前必须执行这个过程,具体操作包括设置实例中每个...
    莽原奔马668阅读 701评论 0 3
  • 构造过程 构造过程是使用类、结构体或枚举类型的实例之前的准备过程。在新实例可用前必须执行这个过程,具体操作包括设置...
    蛊毒_阅读 739评论 0 2