【Mybatis】LambdaQueryWrapper用法示例
本文适用于SpringBoot2.5.4,Mybatis2.2.0,mybatis-plus3.0.5版本,不保证在其它版本的适用性。
例程下载:https://files.cnblogs.com/files/heyang78/redisCache_LambdaQueryWrapper_211005.rar
mybatis-plus提供的LambdaQueryWrapper最大好处是省去书写一些简单SQL的工夫,弊端是有被滥用的可能,背离了lamda的初衷。
当然本文的目的是讲用法,用得适当与否不在探讨之列。
第一步:增加依赖:
<!-- Spring MyBatis Support --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
上面红色部分就是要增加的mybatis-plus依赖。
第二步:书写EmpDao类
package com.hy.myapp; import org.apache.ibatis.annotations.Mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @Mapper public interface EmpDao extends BaseMapper<Emp>{ }
这个类够简单的,里面空空如也。
第三步:使用
@RestController public class JsonCtrl { @Resource private EmpDao empDao; @RequestMapping(value="/searchEmpsByAge", method=RequestMethod.GET) public List<Emp> searchEmpsByAge(@ModelAttribute(value="age") int age) { QueryWrapper<Emp> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(Emp::getAge, age); List<Emp> emps=empDao.selectList(queryWrapper); return emps; } ... }
之后用postman调接口就行了:
req:http://localhost:8080/myapp/searchEmpsByAge?age=42
rsp:
[ { "id": 3, "name": "李逵", "age": 42 }, { "id": 1, "name": "2222", "age": 42 }, { "id": 2, "name": "3333", "age": 42 } ]
到这里就完成了。
参考资料:
1.https://www.jb51.net/article/194620.htm
2.https://blog.csdn.net/liuyishan1993/article/details/92794854
3.https://blog.csdn.net/qq_28336351/article/details/82635100
在此感谢以上作者的付出。
END
分类:
Java.Mybatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2019-10-05 [MyBatis]完整MyBatis CRUD工程
2019-10-05 [log4j]Error:The method getLogger(String) in the type Logger is not applicable for the arguments
2019-10-05 [java]将秒数转化为“天时分秒”的格式(转贴+修改)
2019-10-05 [Java]简单计算下一段Java代码段运行了多少秒
2019-10-05 【log4j】log4j.properties 文件示例
2019-10-05 [MyBatis]最简MyBatis工程
2019-10-05 [Java]给指定时间加上十秒