上一节我们完成了基于 mybatis-plus 的CRUD操作,这一节我们来学习一下使用 mybatis-plus 中的条件构造器—— AbstractWrapper ,我们主要使用的是 QueryWrapper 来演示,其他的大家自己可以尝试。
首先我们来介绍一下 AbstractWrapper ,下图是 AbstractWrapper 的一个继承结构:
Mybatis-Plus 通过 QueryWrapper ( MP 封装的一个查询条件构造器,继承自 AbstractWrapper , AbstractWrapper 实现了 Wrapper 等接口) 来让用户自由的构建查询条件,简单便捷,没有额外的负担,能够有效提高开发效率 QueryWrapper , 主要用于处理 sql 拼接,排序,实体参数查询等 | 查询方式 | 说明 |
|---|---|
| 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的演示:
/**
* 条件构造器 更新操作
*/
@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);
}
复制代码
/**
* 条件构造器 删除操作
*/
@Test
public void testWrapperDelete() {
employeeMapper.delete(
new QueryWrapper<Employee>()
.eq("age", 22)
.eq("last_name", "MP")
);
}
复制代码
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的代码结构如下所示:
至此,基于 mybatis-plus 的条件构造器——QueryWrapper演示就完成了,下面我们就可以进入到下一节 ActiveRecord(活动记录) 了。