上一节咱们完成了基于mybatis-plus
的CRUD操做,这一节咱们来学习一下使用mybatis-plus
中的条件构造器——AbstractWrapper
,咱们主要使用的是QueryWrapper
来演示,其余的你们本身能够尝试。java
首先咱们来介绍一下AbstractWrapper
,下图是AbstractWrapper
的一个继承结构:mysql
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
文件:git
<?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的演示:github
/**
* 条件构造器 更新操做
*/
@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的代码结构以下所示:spring
至此,基于 mybatis-plus
的条件构造器——QueryWrapper演示就完成了,下面咱们就能够进入到下一节ActiveRecord(活动记录)
了。sql
相关示例完整代码:mybatis-plus-in-action数据库