Mybatis Plus为我们提供了如下的一些条件构造器,我们可以利用它们实现查询条件、删除条件、更新条件的构造。
条件构造器 | MyBatis-Plus (baomidou.com)
com.baomidou
mybatis-plus-boot-starter
3.1.2
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
# 配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.161.3:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
username:
password:
# 配置日志
logging:
level:
root: warn
com.zimug.boot.launch.mapper: trace
@SpringBootApplication
@MapperScan(basePackages = {"com.zimug.boot.launch.mapper"})
public class BootLaunchApplication {
public static void main(String[] args) {
SpringApplication.run(BootLaunchApplication.class, args);
}
}
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
编写实体类User.java。
@Data //lombok注解
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写Mapper类UserMapper.java
public interface UserMapper extends BaseMapper {
}
UpdateWrapper update = new UpdateWrapper<>();
update.eq("name", "Jack").eq("age", 28); //eq是MP的条件构造器,表示"等于"关系
User user = new User();
user.setAge(29);
user.setEmail("[email protected]");
int rows = userMapper.update(user, update);
System.out.println("影响记录数:" + rows);
QueryWrapper query = new QueryWrapper<>();
query.like("name", "J") //like是MP的条件构造器,表示"模糊查询"
.lt("age", 40) //lt是MP的条件构造器,表示"小于"关系
.select("name", "age");
List
QueryWrapper query = new QueryWrapper<>();
query.select("name", "age") //指定查询结果字段
.in("age", Arrays.asList(30, 31, 34, 35))
.last("limit 1");
List list = userMapper.selectList(query);
list.forEach(System.out::println);
String name = "字母"; //name不为空
String email = ""; //email为空串
QueryWrapper query = new QueryWrapper<>();
query.like(StringUtils.isNotEmpty(name), "name", name)
//因为email为空串,该条件未生效
.like(StringUtils.isNotEmpty(email), "email", email);
List list = userMapper.selectList(query);
list.forEach(System.out::println);
所以最终的执行SQL,如下(只有name LIKE条件,没有email LIKE 条件):
SELECT id,name,age,email
FROM user
WHERE name LIKE %字母%
关于 like 几乎所有的条件构造函数,都提供了condition参数实现动态SQL。也就是参数判断是否返回true,如果返回false,该条件不成立。如email=“”
,所以.like(StringUtils.isNotEmpty(email), "email", email);
的条件不成立。
//构造条件
QueryWrapper query = new QueryWrapper<>();
Map params = new HashMap<>();
params.put("name", "字母哥");
params.put("age", 18);
params.put("email", null);
// query.allEq(params,false);
query.allEq((k, v) -> !k.equals("name"), params, false);
List list = userMapper.selectList(query);
list.forEach(System.out::println);
all表示所有 ;Eq是equal的缩写表示相等关系 。最终执行的SQL如下:
SELECT id,name,age,email
FROM user
WHERE age = ?
params.put("name", "字母哥");
这个查询条件被过滤掉params.put("email", null);
这个查询条件被过滤掉// LambdaQueryWrapper lambdaQ = new QueryWrapper().lambda();
// LambdaQueryWrapper lambdaQ = new LambdaQueryWrapper<>();
LambdaQueryWrapper lambdaQ = Wrappers.lambdaQuery();
lambdaQ.like(User::getName, "字母")
.lt(User::getAge, 18);
List list = userMapper.selectList(lambdaQ);
lambda条件构造器,最终执行SQL如下:
SELECT id,name,age,email
FROM user
WHERE name LIKE %字母%
AND age < 18
List list = new LambdaQueryChainWrapper(userMapper)
.likeRight(User::getName, "字母")
.and(q -> q.lt(User::getAge, 40)
.or()
.isNotNull(User::getEmail)
)
.list();
list.forEach(System.out::println);
lambda条件构造器,最终执行SQL如下:
SELECT id,name,age,email
FROM user
WHERE name LIKE '字母%'
AND ( age < 40 OR email IS NOT NULL )