四、条件构造器——AbstractWrapper

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/runewbie/article/details/102759008

上一节我们完成了基于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一节的操作,新建一个mp03 的 Module,可以将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("[email protected]");
    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("[email protected]");
        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

猜你喜欢

转载自blog.csdn.net/runewbie/article/details/102759008
今日推荐