引言
是对MyBatis-Plus的功能进行简单介绍,虽然是介绍,也让我们领略到他的优雅与强大。你是不是已经被吸引了?别着急,上一节,我们算是参观了MyBatis的风景,这一节,我将带你领略他独特的魅力。
Lambda
官方表示,3.x支持Lambda表达式,那应该怎么使用呢?我们来看个例子:
1
2
3
4
5
|
QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(Student::getName, "冯文议" ); List<Student> studentList = list(queryWrapper); for (Student student : studentList) Console.info(student); |
看一下测试结果(为了看好,我们转成json):
{
"id":1035789714459471874,
"name":"冯文议",
"age":26,
"info":"无畏造英雄",
"isDelete":false,
"createTime":"Sep 1, 2018 3:21:26 PM",
"updateTime":"Sep 1, 2018 3:21:26 PM",
"gender":"MALE",
"idcardId":1035789714388168706,
"cityId":1035762001753501698
}
如果你使用了我的配置,你也能看到相应的SQL
==> Preparing: SELECT id,name,age,info,is_delete,create_time,update_time,gender,idcard_id,city_id FROM t_student WHERE name = ?
==> Parameters: 冯文议(String)
<== Columns: id, name, age, info, is_delete, create_time, update_time, gender, idcard_id, city_id
<== Row: 1035789714459471874, 冯文议, 26, <<BLOB>>, 0, 2018-09-01 15:21:26.0, 2018-09-01 15:21:26.0, 1, 1035789714388168706, 1035762001753501698
<== Total: 1
分页查询
感觉哈,分页查询是他们框架的起因,那我们先说分页查询。直接看代码:
第一步:在 Application 中配置
1
2
3
4
5
6
7
|
/** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } |
第二步:写分页代码(为了你能够看得清楚,我截图给你):
看结果(json):
{
"records":[
{
"id":1035788325322752001,
"name":"1",
"age":1,
"info":"1",
"isDelete":false,
"createTime":"Sep 1, 2018 3:15:55 PM",
"updateTime":"Sep 1, 2018 3:15:55 PM",
"gender":"MALE",
"idcardId":1035788325276614657,
"cityId":1035788325201117185
},
{
"id":1035789714459471874,
"name":"冯文议",
"age":26,
"info":"无畏造英雄",
"isDelete":false,
"createTime":"Sep 1, 2018 3:21:26 PM",
"updateTime":"Sep 1, 2018 3:21:26 PM",
"gender":"MALE",
"idcardId":1035789714388168706,
"cityId":1035762001753501698
}
],
"total":2,
"size":2,
"current":1,
"optimizeCountSql":true
}
不要问我前端应该怎么写,表示我也不会写。
条件查询
终于要进入这里了,是不是很激动啊。别急,客官,抽根烟先,我们慢慢来。
【1】多eq
1
2
3
4
5
6
7
|
QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda() .eq(Student::getName, "冯文议" ) .eq(Student::getAge, 26 ); List<Student> studentList = list(queryWrapper); for (Student student : studentList) Console.info( new Gson().toJson(student)); |
对于这部分的测试,我想结果是毫无因为,那么你应该关注什么呢?没错,SQL,所以,我们直接看SQL。当然,结果也是可以看到的。
==> Preparing: SELECT id,name,age,info,is_delete,create_time,update_time,gender,idcard_id,city_id FROM t_student WHERE name = ? AND age = ?
==> Parameters: 冯文议(String), 26(Integer)
<== Columns: id, name, age, info, is_delete, create_time, update_time, gender, idcard_id, city_id
<== Row: 1035789714459471874, 冯文议, 26, <<BLOB>>, 0, 2018-09-01 15:21:26.0, 2018-09-01 15:21:26.0, 1, 1035789714388168706, 1035762001753501698
<== Total: 1
我们还可以这样写:
1
2
3
4
5
6
7
8
9
|
QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda() .and(obj -> obj.eq(Student::getName, "冯文议" ) .eq(Student::getAge, 26 )); List<Student> studentList = list(queryWrapper); for (Student student : studentList) Console.info( new Gson().toJson(student)); |
【2】or
第一种:
1
2
3
4
5
6
7
|
QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda() .or(obj1 -> obj1.eq(Student::getName, "冯文议" )) .or(obj2 -> obj2.eq(Student::getName, "1" )); List<Student> studentList = list(queryWrapper); for (Student student : studentList) Console.info( new Gson().toJson(student)); |
sql:
1
|
SELECT * FROM t_student WHERE ( name = ? ) OR ( name = ? ) |
第二种:
1
2
3
4
5
6
7
8
|
QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda() .eq(Student::getName, "冯文议" ) .or() .eq(Student::getName, "1" ); List<Student> studentList = list(queryWrapper); for (Student student : studentList) Console.info( new Gson().toJson(student)); |
SQL:
1
|
SELECT * FROM t_student WHERE name = ? OR name = ? |
这样的话,我们就可以拼接各种条件了。那么问题来了:到底有哪些关键字呢?性能如何呢?
条件构造器
条件参数说明
查询方式 | 说明 |
---|---|
setSqlSelect | 设置 SELECT 查询字段 |
where |
WHERE 语句,拼接 + WHERE 条件 |
and |
AND 语句,拼接 + AND 字段=值 |
andNew |
AND 语句,拼接 + AND (字段=值) |
or |
OR 语句,拼接 + OR 字段=值 |
orNew |
OR 语句,拼接 + OR (字段=值) |
eq | 等于= |
allEq | 基于 map 内容等于= |
ne | 不等于<> |
gt | 大于> |
ge | 大于等于>= |
lt | 小于< |
le | 小于等于<= |
like | 模糊查询 LIKE |
notLike | 模糊查询 NOT LIKE |
in | IN 查询 |
notIn | NOT IN 查询 |
isNull | NULL 值查询 |
isNotNull | IS NOT NULL |
groupBy | 分组 GROUP BY |
having | HAVING 关键词 |
orderBy | 排序 ORDER BY |
orderAsc | ASC 排序 ORDER BY |
orderDesc | DESC 排序 ORDER BY |
exists | EXISTS 条件语句 |
notExists | NOT EXISTS 条件语句 |
between | BETWEEN 条件语句 |
notBetween | NOT BETWEEN 条件语句 |
addFilter | 自由拼接 SQL |
last | 拼接在最后,例如:last(“LIMIT 1”) |
注意! xxNew 都是另起 ( ... )
括号包裹。
自定义sql
如果官方提供的满足不了你的需求,或者你的需求很复杂,导致你不知道如何使用条件构造器,那应该怎么办呢?
很简单。
第一步:找到 Dao,写一个数据库操作接口
1
2
3
4
5
|
public interface StudentDao extends BaseMapper<Student> { List<Student> selectAll(); } |
第二步:在xml文件中写sql
1
2
3
4
|
<!--List<Student> selectAll();--> < select id = "selectAll" resultMap = "BaseResultMap" > select * from t_student </ select > |
这样我们就可以使用了:
1
2
3
4
5
6
|
@Resource StudentDao studentDao; List<Student> studentList = studentDao.selectAll(); for (Student student : studentList) Console.info( new Gson().toJson(student)); |
测试:
封装我们自己的Service
前面我们就说了,我是很不喜欢MP的查询接口的,我们就把他弄成我们喜欢的吧,我这里借鉴 JPA接口了,哈哈
interface:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 查询所有数据 * @return List<Student> */ List<Student> findAll(); /** * 查询部分数据 * @return List<Student> */ List<Student> findList(); /** * 查询一条数据 * @return Student */ Student findOne(); /** * 根据主键ID查询数据 * @param id 主键ID,为null,返回null * @return Student */ Student findById(Long id); |
impl:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Override public List<Student> findAll() { return list( null ); } @Override public List<Student> findList() { return list( null ); } @Override public Student findOne() { return getOne( null ); } @Override public Student findById(Long id) { ExceptionUtil.notNull(id, "id must not null." ); return getById(id); } |
我们来试一下:
哇!!!
是不是很爽!!!
资料
[1] MyBatis-Plus测试示例
[2] 官网测试例子:WrapperTest.java
总结
到此这篇关于MyBatis-Plus中最简单的查询操作的文章就介绍到这了,更多相关MyBatis-Plus查询操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_28336351/article/details/82635100