Mybatis-Plus使用条件构造器(QueryWrapper)进行条件查询的9个小例子
首先创建user表
CREATE TABLE user (
id BIGINT ( 20 ) PRIMARY KEY NOT NULL COMMENT '主键',
name VARCHAR ( 30 ) DEFAULT NULL COMMENT '姓名',
age INT ( 11 ) DEFAULT NULL COMMENT '年龄',
email VARCHAR ( 50 ) DEFAULT NULL COMMENT '邮箱',
manager_id BIGINT ( 20 ) DEFAULT NULL COMMENT '直属上级id',
create_time DATETIME DEFAULT NULL COMMENT '创建时间',
CONSTRAINT manager_fk FOREIGN KEY ( manager_id ) REFERENCES USER ( id )
) ENGINE = INNODB CHARSET = UTF8;
插入几条数据
INSERT INTO `user` ( id, name, age, email, manager_id, create_time )
VALUES
( 1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL, '2019-01-11 14:20:20' ),
( 1088248166370832385, '王天风', 25, 'wtf@baomidou.com', 1087982257332887553, '2019-02-05 11:12:22' ),
( 1088250446457389058, '李艺伟', 28, 'lyw@baomidou.com', 1088248166370832385, '2019-02-14 08:31:16' ),
( 1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385, '2019-01-14 09:15:15' ),
( 1094592041087729666, '刘红雨', 31, 'lhm@baomidou.com', 1088248166370832385, '2019-01-14 09:48:16' );
-
名字中包含“雨”并且年龄小于40
name like '%雨%' and age<40
@Test
public void selectByWrapper1() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// lt为小于的意思
queryWrapper.like("name", "雨").lt("age", 40);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ?
DEBUG==> Parameters: %雨%(String), 40(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 2
-
名字中包含“雨”并且年龄大于等于20且小于等于40并且email不为空
name like '%雨%' and age between 20 and 40 and email is not null
@Test
public void selectByWrapper2() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "雨").between("age", 20, 40).isNotNull("email");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL
DEBUG==> Parameters: %雨%(String), 20(Integer), 40(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 2
-
名字为“王”姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列
name like '王%' or age>=25 order by age desc,id,asc
@Test
public void selectByWrapper3() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// ge为大于等于的意思
queryWrapper.likeRight("name", "王").or().ge("age", 25)
.orderByDesc("age").orderByAsc("id");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? OR age >= ? ORDER BY age DESC , id ASC
DEBUG==> Parameters: 王%(String), 25(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1087982257332887553, 大boss, 40, boss@baomidou.com, null, 2019-01-11 14:20:20
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
TRACE<== Row: 1088250446457389058, 李艺伟, 28, lyw@baomidou.com, 1088248166370832385, 2019-02-14 08:31:16
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
DEBUG<== Total: 5
-
创建日期为2019年2月14日并且直属上级名字为王姓
date_format(create_time,'%Y-%m-%d') and manager_id in (select id from user where name like '王%')
@Test
public void selectByWrapper4() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14")
.inSql("manager_id", "select id from user where name like '王%'");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE date_format(create_time,'%Y-%m-%d')=? AND manager_id IN (select id from user where name like '王%')
DEBUG==> Parameters: 2019-02-14(String)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088250446457389058, 李艺伟, 28, lyw@baomidou.com, 1088248166370832385, 2019-02-14 08:31:16
DEBUG<== Total: 1
-
名字为“王”姓并且(年龄小于40或邮箱不为空)
name like '王%' and (age<40 or email is not null)
@Test
public void selectByWrapper5() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "王")
.and(wq -> wq.lt("age", 40).or().isNotNull("email"));
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND ( age < ? OR email IS NOT NULL )
DEBUG==> Parameters: 王%(String), 40(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
DEBUG<== Total: 1
-
名字为“王”姓或者(年龄小于40并且年龄大于20并且邮箱不为空)
name like '王%' or (age<40 and age>20 and email is not null)
@Test
public void selectByWrapper6() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "王")
.or(wq -> wq.lt("age", 40).gt("age", 20).isNotNull("email"));
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? OR ( age < ? AND age > ? AND email IS NOT NULL )
DEBUG==> Parameters: 王%(String), 40(Integer), 20(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
TRACE<== Row: 1088250446457389058, 李艺伟, 28, lyw@baomidou.com, 1088248166370832385, 2019-02-14 08:31:16
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 4
-
(年龄小于40或邮箱不为空)并且名字为王姓
(age<40 or email is not null) and name like '王%'
@Test
public void selectByWrapper7() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.nested(wq -> wq.lt("age", 40).or().isNotNull("email"))
.likeRight("name", "王");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE ( age < ? OR email IS NOT NULL ) AND name LIKE ?
DEBUG==> Parameters: 40(Integer), 王%(String)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
DEBUG<== Total: 1
-
年龄为30,31,34,35
age in (30,31,34,35)
@Test
public void selectByWrapper8() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.in("age", Arrays.asList(30, 31, 34, 35));
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE age IN (?,?,?,?)
DEBUG==> Parameters: 30(Integer), 31(Integer), 34(Integer), 35(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 2
-
只返回满足条件的其中一条语句即可
limit 1
@Test
public void selectByWrapper9() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 无视优化规则直接拼接到 sql 的最后
// 注意事项:
// 只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
queryWrapper.in("age", Arrays.asList(30, 31, 34, 35)).last("limit 1");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
日志输出sql:
DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE age IN (?,?,?,?) limit 1
DEBUG==> Parameters: 30(Integer), 31(Integer), 34(Integer), 35(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
DEBUG<== Total: 1