MyBatis-Plus | 最简单的查询操作教程(Lambda)
引言
上一篇:MyBatis-Plus | 最优雅最简洁地完成数据库操作
是对MyBatis-Plus的功能进行简单介绍,虽然是介绍,也让我们领略到他的优雅与强大。你是不是已经被吸引了?别着急,上一节,我们算是参观了MyBatis的风景,这一节,我将带你领略他独特的魅力。
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 = ?
这样的话,我们就可以拼接各种条件了。那么问题来了:到底有哪些关键字呢?性能如何呢?
条件构造器
查询方式 | 说明 |
---|---|
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,写一个数据库操作接口
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));
测试:
封装我们自己的Service
前面我们就说了,我是很不喜欢MP的查询接口的,我们就把他弄成我们喜欢的吧,我这里借鉴 JPA接口了,哈哈
interface:
/** * 查询所有数据 * @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:
@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
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
java源码-BufferedWriter
开篇 BufferedWriter和BufferedReader是对称的,一并分析下源码。 源码分析 构造函数 BufferedWriter的构造函数通过缓冲区char cb[]数组保存待持久化字符 缓存区默认大小为8192 BufferedWriter跟BufferedReader一样采用装饰者模式进行设计,通过内部Writer out对象保存被装饰的Writer对象。 BufferedWriter的写操作通过被装饰的out对象进行数据写入。 public class BufferedWriter extends Writer { private Writer out; private char cb[]; private int nChars, nextChar; private static int defaultCharBufferSize = 8192; private String lineSeparator; public BufferedWriter(Writer out) { this(out, defaultCharBufferSize); } public Bu...
- 下一篇
div图片和内容都居中应用
width:85%; margin:0 auto; }.parent p{ line-height:30px; text-indent:30px; }.parent img{ max-width:100%; height:auto; } <?php echo $GLOBALS['Content']; ?> 返回
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Red5直播服务器,属于Java语言的直播服务器
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主