目录
前言
Mybatis-Plus是基于sping-boot项目 如果还没有配置的先去看下面这篇博客
- ## 使用库
- USE mp;
- ## 清空表
- TRUNCATE TABLE tbl_employee;
- ## 导入数据
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan0','123@qq.com',0,21);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan1','123@qq.com',0,22);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan2','123@qq.com',0,23);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan3','123@qq.com',0,24);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan4','123@qq.com',0,25);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan5','123@qq.com',0,26);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan6','123@qq.com',0,27);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan7','123@qq.com',0,28);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan8','123@qq.com',0,29);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan9','123@qq.com',0,30);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby0','123@qq.com',1,21);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby1','123@qq.com',0,22);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby2','123@qq.com',1,23);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby3','123@qq.com',0,24);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby4','123@qq.com',1,25);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby5','123@qq.com',0,26);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby6','123@qq.com',1,27);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby7','123@qq.com',0,28);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby8','123@qq.com',1,29);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby9','123@qq.com',0,30);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom0','123@qq.com',1,21);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom1','123@qq.com',0,22);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom2','123@qq.com',1,23);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom3','123@qq.com',0,24);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom4','123@qq.com',1,25);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom5','123@qq.com',0,26);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom6','123@qq.com',1,27);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom7','123@qq.com',0,28);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom8','123@qq.com',1,29);
- INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom9','123@qq.com',0,30);
- ## 查询数据
- SELECT * FROM tbl_employee;
MyBatis-Plus 通过 EntityWrapper(简称 EW,MP 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率,它主要用于处理 sql 拼接,排序,实体参数查询等。
注意:使用的是数据库字段,不是 Java 属性!
警告:MyBatis-Plus不支持以及不赞成在 RPC 调用中把 Wrapper 进行传输,Wrapper 很重,传输 Wrapper 可以类比为你的 controller 用 map 接收值(开发一时爽,维护火葬场),正确的 RPC 调用姿势是写一个 DTO 进行传输,被调用方再根据 DTO 执行相应的操作。
需求描述:查询所有姓名的包含B、且姓名为女(1)、且年龄大于24岁的员工信息
- @Test
- void testSelectList1() {
- QueryWrapper
queryWrapper = new QueryWrapper<>(); - queryWrapper
- .like("last_name","B")
- .eq("gender",1)
- .gt("age",24);
- List
employees = employeeMapper.selectList(queryWrapper); - employees.forEach(System.out::println);
- }
需求描述:查询所有员工信息
- @Test
- void testSelectList2() {
- List
employees = employeeMapper.selectList(null); - employees.forEach(System.out::println);
- }
需求描述:查询所有女生的数量(1)
- @Test
- void testSelectList3() {
- QueryWrapper
queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("gender", 1);
- Integer count = employeeMapper.selectCount(queryWrapper);
- System.out.println(count);
- }
需求信息:将年龄大于25岁的女生(1)的性别修改为男生(0)
- @Test
- void testUpdate() {
- UpdateWrapper
updateWrapper = new UpdateWrapper<>(); - updateWrapper
- .eq("gender", 1)
- .gt("age", 25)
- ;
- Employee employee = new Employee();
- employee.setGender(0);
- employeeMapper.update(employee, updateWrapper);
- }
- @Test
- void testDelete() {
- QueryWrapper
queryWrapper = new QueryWrapper<>(); - queryWrapper.like("last_name", "Tom");
- int result = employeeMapper.delete(queryWrapper);
- System.out.println(result);
- }
参数说明:
AbstractWrapper:
说明:AbstractWrapper 是 QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件,entity 属性也用于生成 sql 的 where 条件,注意 entity 生成的 where 条件与使用各个 api 生成的 where 条件没有任何关联行为
- allEq(Map
params) - allEq(Map
params, boolean null2IsNull) - allEq(boolean condition, Map
params, boolean null2IsNull)
eq
(或个别 isNull
)个别参数说明:
params
:key
为数据库字段名,value
为字段值null2IsNull
:为true
则在map
的value
为null
时调用 isNull
方法为false
时则忽略value
为null
的
- allEq(BiPredicate
filter, Map params) - allEq(BiPredicate
filter, Map params, boolean null2IsNull) - allEq(boolean condition, BiPredicate
filter, Map params, boolean null2IsNull)
个别参数说明:
filter
: 过滤函数,是否允许字段传入比对条件中params
与 null2IsNull
: 同上
- eq(R column, Object val)
- eq(boolean condition, R column, Object val)
eq("name", "老王")
—>name = '老王'
- ne(R column, Object val)
- ne(boolean condition, R column, Object val)
ne("name", "老王")
—>name <> '老王'
- gt(R column, Object val)
- gt(boolean condition, R column, Object val)
gt("age", 18)
—>age > 18
- ge(R column, Object val)
- ge(boolean condition, R column, Object val)
ge("age", 18)
—>age >= 18
- lt(R column, Object val)
- lt(boolean condition, R column, Object val)
lt("age", 18)
—>age < 18
- le(R column, Object val)
- le(boolean condition, R column, Object val)
le("age", 18)
—>age <= 18
- between(R column, Object val1, Object val2)
- between(boolean condition, R column, Object val1, Object val2)
between("age", 18, 30)
—>age between 18 and 30
- notBetween(R column, Object val1, Object val2)
- notBetween(boolean condition, R column, Object val1, Object val2)
notBetween("age", 18, 30)
—>age not between 18 and 30
- like(R column, Object val)
- like(boolean condition, R column, Object val)
like("name", "王")
—>name like '%王%'
- notLike(R column, Object val)
- notLike(boolean condition, R column, Object val)
notLike("name", "王")
—>name not like '%王%'
- likeLeft(R column, Object val)
- likeLeft(boolean condition, R column, Object val)
likeLeft("name", "王")
—>name like '%王'
- likeRight(R column, Object val)
- likeRight(boolean condition, R column, Object val)
likeRight("name", "王")
—>name like '王%'
- isNull(R column)
- isNull(boolean condition, R column)
isNull("name")
—>name is null
- isNotNull(R column)
- isNotNull(boolean condition, R column)
isNotNull("name")
—>name is not null
- in(R column, Collection<?> value)
- in(boolean condition, R column, Collection<?> value)
in("age",{1,2,3})
—>age in (1,2,3)
- in(R column, Object... values)
- in(boolean condition, R column, Object... values)
in("age", 1, 2, 3)
—>age in (1,2,3)
- notIn(R column, Collection<?> value)
- notIn(boolean condition, R column, Collection<?> value)
notIn("age",{1,2,3})
—>age not in (1,2,3)
- inSql(R column, String inValue)
- inSql(boolean condition, R column, String inValue)
- notInSql(R column, String inValue)
- notInSql(boolean condition, R column, String inValue)
- groupBy(R... columns)
- groupBy(boolean condition, R... columns)
groupBy("id", "name")
—>group by id,name
- orderByAsc(R... columns)
- orderByAsc(boolean condition, R... columns)
orderByAsc("id", "name")
—>order by id ASC,name ASC
- orderByDesc(R... columns)
- orderByDesc(boolean condition, R... columns)
orderByDesc("id", "name")
—>order by id DESC,name DESC
orderBy(boolean condition, boolean isAsc, R... columns)
orderBy(true, true, "id", "name")
—>order by id ASC,name ASC
- having(String sqlHaving, Object... params)
- having(boolean condition, String sqlHaving, Object... params)
having("sum(age) > 10")
—>having sum(age) > 10
having("sum(age) > {0}", 11)
—>having sum(age) > 11
- func(Consumer<Children> consumer)
- func(boolean condition, Consumer<Children> consumer)
func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
- or()
- or(boolean condition)
注意事项:
主动调用or
表示紧接着下一个方法不是用and
连接!(不调用or
则默认为使用and
连接)
eq("id",1).or().eq("name","老王")
—>id = 1 or name = '老王'
- or(Consumer<Param> consumer)
- or(boolean condition, Consumer<Param> consumer)
or(i -> i.eq("name", "李白").ne("status", "活着"))
—>or (name = '李白' and status <> '活着')
- and(Consumer<Param> consumer)
- and(boolean condition, Consumer<Param> consumer)
and(i -> i.eq("name", "李白").ne("status", "活着"))
—>and (name = '李白' and status <> '活着')
- nested(Consumer<Param> consumer)
- nested(boolean condition, Consumer<Param> consumer)
nested(i -> i.eq("name", "李白").ne("status", "活着"))
—>(name = '李白' and status <> '活着')
- apply(String applySql, Object... params)
- apply(boolean condition, String applySql, Object... params)
注意事项:
该方法可用于数据库函数动态入参的params
对应前面applySql
内部的{index}
部分,这样是不会有sql注入风险的,反之会有!
- last(String lastSql)
- last(boolean condition, String lastSql)
注意事项:
只能调用一次,多次调用以最后一次为准,有sql注入的风险,请谨慎使用
last("limit 1")
- exists(String existsSql)
- exists(boolean condition, String existsSql)
exists("select id from table where age = 1")
—>exists (select id from table where age = 1)
- notExists(String notExistsSql)
- notExists(boolean condition, String notExistsSql)
notExists("select id from table where age = 1")
—>not exists (select id from table where age = 1)