Lambda
官方表示,3.x支持Lambda表达式,那应该怎么使用呢?我们来看个例子:
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 中配置
/**
* 分页插件
*/
@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
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
我们还可以这样写:
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
第一种:
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:
SELECT * FROM t_student WHERE ( name = ? ) OR ( name = ? )
第二种:
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:
SELECT * FROM t_student WHERE name = ? OR name = ?
这样的话,我们就可以拼接各种条件了。那么问题来了:到底有哪些关键字呢?性能如何呢?
条件构造器
自定义sql
如果官方提供的满足不了你的需求,或者你的需求很复杂,导致你不知道如何使用条件构造器,那应该怎么办呢?
很简单。
第一步:找到 Dao,写一个数据库操作接口
public interface StudentDao extends BaseMapper<Student> {
List<Student> selectAll();
}
第二步:在xml文件中写sql
<!--List<Student> selectAll();-->
<select id="selectAll" resultMap="BaseResultMap">
select * from t_student
</select>
这样我们就可以使用了:
@Resource
StudentDao studentDao;
List<Student> studentList = studentDao.selectAll();
for (Student student : studentList)
Console.info(new Gson().toJson(student));
转载自:https://blog.csdn.net/qq_28336351/article/details/82635100