【Springboot学习】从零开始学习Springboot(七)
以下记录皆基于MybatisPlus。
查看标准日志
标准日志可以查看sql执行记录,在配置文件中配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
效果
分页查询
-
定义分页对象Ipage
@Test void testPage() { IPage page = new Page(1,5); bookDao.selectPage(page,null); }
- 使用MP的拦截器实现分页(分页的实际上就是动态拼接sql语句)
@Configuration public class MPConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ //定义MP拦截器 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //添加具体的拦截器,这里是分页拦截器 interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } }
条件查询
条件查询支持动态拼接条件,如 bookLambdaQueryWrapper.like(name!=null,Book::getName,"Spring");
,那么name不为空时将会返回ture,进行拼接
使用QueryWrapper进行查询
直接使用键名,容易出现错误
@Test
void testGetByCondition() {
QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","Spring");
bookDao.selectList(queryWrapper);
}
使用LambdaQueryWrapper进行查询
使用get方法取键名,不容易出错,推荐使用这种方法
@Test
void testGetByCondition() {
String name = null;
LambdaQueryWrapper<Book> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
bookLambdaQueryWrapper.like(name!=null,Book::getName,"Spring");
bookDao.selectList(bookLambdaQueryWrapper);
}
本文作者:Texley
本文链接:https://www.cnblogs.com/texley/p/16462236.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端