是对MyBatis-Plus的功能进行简单介绍,虽然是介绍,也让我们领略到他的优雅与强大。你是不是已经被吸引了?别着急,上一节,我们算是参观了MyBatis的风景,这一节,我将带你领略他独特的魅力。
官方表示,3.x支持Lambda表达式,那应该怎么使用呢?我们来看个例子:
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(Student::getName, "小小");
List 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, <>, 0, 2018-09-01 15:21:26.0, 2018-09-01 15:21:26.0, 1, 1035789714388168706, 1035762001753501698
<== Total: 1
感觉哈,分页查询是他们框架的起因,那我们先说分页查询。直接看代码:
第一步:在 Application
中配置
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
看看 JSON 数据:
{
"records":[
{
"id":1035788325322752001,
"name":"张飞",
"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
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.eq(Student::getName, "西夏")
.eq(Student::getAge, 26);
List 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, <>, 0, 2018-09-01 15:21:26.0, 2018-09-01 15:21:26.0, 1, 1035789714388168706, 1035762001753501698
<== Total: 1
我们还可以这样写:
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.and(obj ->
obj.eq(Student::getName, "西夏")
.eq(Student::getAge, 26));
List studentList = list(queryWrapper);
for (Student student : studentList)
Console.info(new Gson().toJson(student));
【2】or
第一种:
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.or(obj1 -> obj1.eq(Student::getName, "西夏"))
.or(obj2 -> obj2.eq(Student::getName, "1"));
List studentList = list(queryWrapper);
for (Student student : studentList)
Console.info(new Gson().toJson(student));
sql:
SELECT * FROM t_student WHERE ( name = ? ) OR ( name = ? )
第二种:
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.eq(Student::getName, "西夏")
.or()
.eq(Student::getName, "1");
List studentList = list(queryWrapper);
for (Student student : studentList)
Console.info(new Gson().toJson(student));
SQL:
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 都是另起 ( ... )
括号包裹。
如果官方提供的满足不了你的需求,或者你的需求很复杂,导致你不知道如何使用条件构造器,那应该怎么办呢?
很简单。
第一步:找到 Dao,写一个数据库操作接口
public interface StudentDao extends BaseMapper {
List selectAll();
}
第二步:在xml文件中写sql
这样我们就可以使用了:
@Resource
StudentDao studentDao;
List studentList = studentDao.selectAll();
for (Student student : studentList){
Console.info(new Gson().toJson(student));
}
测试:
前面我们就说了,我是很不喜欢MP的查询接口的,我们就把他弄成我们喜欢的吧,我这里借鉴 JPA接口了,哈哈
interface:
/**
* 查询所有数据
* @return List
*/
List findAll();
/**
* 查询部分数据
* @return List
*/
List findList();
/**
* 查询一条数据
* @return Student
*/
Student findOne();
/**
* 根据主键ID查询数据
* @param id 主键ID,为null,返回null
* @return Student
*/
Student findById(Long id);
impl:
@Override
public List findAll() {
return list(null);
}
@Override
public List 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