接着我上一章:Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下
上一章我把项目做了一部分工程化包装,主要还是想要之后的调试能够方便一些。那么这一章接着上一章的内容,做一下查询分页,并且将每次请求所调用的sql语句写在日志里面,便于我们观察定位问题。代码之后还是会上传到github。
GitHub代码仓库地址:GitHub仓库
简单的准备了一些数据。
之前sql日志一直没有配置,后面随着使用的语句愈发复杂,决定先把日志配置上。方便调试。
这部分和fm没什么关系,如果你会配置的话,可以跳到下个标题。这里把logback.xml发出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | < configuration > < include resource = "org/springframework/boot/logging/logback/defaults.xml" /> < appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender" > < encoder class = "ch.qos.logback.classic.encoder.PatternLayoutEncoder" > < pattern >${CONSOLE_LOG_PATTERN}</ pattern > </ encoder > </ appender > < appender name = "FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < file >/home/work/logs/fmp/log.log</ file > < rollingPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > < fileNamePattern >/home/work/logs/fmp/log.%d{yyyy-MM-dd}.%i.log.gz</ fileNamePattern > < maxFileSize >500MB</ maxFileSize > < maxHistory >7</ maxHistory > </ rollingPolicy > < encoder class = "ch.qos.logback.core.encoder.LayoutWrappingEncoder" > < layout class = "ch.qos.logback.classic.PatternLayout" > < pattern >${FILE_LOG_PATTERN}</ pattern > </ layout > </ encoder > </ appender > < appender name = "ERROR_FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < filter class = "ch.qos.logback.classic.filter.ThresholdFilter" > < level >ERROR</ level > </ filter > < File >/home/work/logs/fmp/error.log</ File > < rollingPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > < fileNamePattern >/home/work/logs/fmp/error.%d{yyyy-MM-dd}.log.%i.log.gz</ fileNamePattern > < maxFileSize >500MB</ maxFileSize > < maxHistory >7</ maxHistory > </ rollingPolicy > < encoder class = "ch.qos.logback.core.encoder.LayoutWrappingEncoder" > < layout class = "ch.qos.logback.classic.PatternLayout" > < pattern >${FILE_LOG_PATTERN}</ pattern > </ layout > </ encoder > </ appender > < appender name = "SQL" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < file >/home/work/logs/fmp/sql.log</ file > < rollingPolicy class = "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > < maxFileSize >500MB</ maxFileSize > < fileNamePattern >/home/work/logs/fmp/sql.%d{yyyy-MM-dd}.%i.log.gz</ fileNamePattern > < maxHistory >7</ maxHistory > </ rollingPolicy > < encoder class = "ch.qos.logback.core.encoder.LayoutWrappingEncoder" > < layout class = "ch.qos.logback.classic.PatternLayout" > < pattern >${FILE_LOG_PATTERN}</ pattern > </ layout > </ encoder > </ appender > < logger name = "org.springframework.web" level = "info" /> < logger name = "com.hy.fmp.fluent.mapper" level = "debug" > < appender-ref ref = "SQL" /> </ logger > < root level = "info" > < appender-ref ref = "STDOUT" /> < appender-ref ref = "FILE" /> < appender-ref ref = "ERROR_FILE" /> </ root > </ configuration > |
简单说明一下,主要是下面这部分的配置,将mapper包内的日志打出来。
1 2 3 | < logger name = "com.hy.fmp.fluent.mapper" level = "debug" > < appender-ref ref = "SQL" /> </ logger > |
我这里先按照官方的分页查询使用编写接口。官方提供的写法是Query语句中有limit方法,和mysql的limit关键字一致。
先定义分页实体
1 2 3 4 5 6 7 8 9 10 11 12 13 | import lombok.Data; /** * @Program: fluent-mybatis-project @ClassName: PageReq @Author: huyi @Date: 2021-10-24 * 23:21 @Description: 分页参数 @Version: V1.0 */ @Data public class PageReq { /** 每页数量 */ private Integer size; /** 页码 */ private Integer page; } |
这里注意,我们要按照limit的语法来,下面是接口方法实现。
参数1:指定要返回的第一行的偏移量。第一行的偏移量为0
,而不是1,所以对应我们的参数就是
1 | pageReq.getPage() * pageReq.getSize() |
参数2:指定要返回的最大行数。
1 | pageReq.getSize() |
1 2 3 4 5 6 7 8 9 | @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public StdPagedList<TestFluentMybatisEntity> selectAllByPage(PageReq pageReq) { return testFluentMybatisMapper.stdPagedEntity( new TestFluentMybatisQuery() .selectAll() .limit(pageReq.getPage() * pageReq.getSize(), pageReq.getSize())); } |
控制层:
1 2 3 4 5 6 7 8 9 10 11 12 | @Autowired private ISelectService selectService; @ApiOperation (value = "分页查询所有数据" , notes = "分页查询所有数据" ) @RequestMapping (value = "/selectAllPage" , method = RequestMethod.POST) @ResponseBody public Result<StdPagedList<TestFluentMybatisEntity>> selectAllPage( @RequestBody PageReq pageReq) { try { return Result.ok(selectService.selectAllByPage(pageReq)); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null ); } } |
看下执行效果
OK,没什么问题。看一下日志打印。
2021-10-25 11:23:55.313 DEBUG 24688 --- [nio-8080-exec-5] c.h.f.f.m.T.countNoLimit : ==> Preparing: SELECT COUNT(*) FROM `test_fluent_mybatis`
2021-10-25 11:23:55.313 DEBUG 24688 --- [nio-8080-exec-5] c.h.f.f.m.T.countNoLimit : ==> Parameters:
2021-10-25 11:23:55.316 DEBUG 24688 --- [nio-8080-exec-5] c.h.f.f.m.T.countNoLimit : <== Total: 1
2021-10-25 11:23:55.317 DEBUG 24688 --- [nio-8080-exec-5] c.h.f.f.m.T.listEntity : ==> Preparing: SELECT `id`, `age`, `create_time`, `del_flag`, `name` FROM `test_fluent_mybatis` LIMIT ?, ?
2021-10-25 11:23:55.317 DEBUG 24688 --- [nio-8080-exec-5] c.h.f.f.m.T.listEntity : ==> Parameters: 2(Integer), 2(Integer)
2021-10-25 11:23:55.319 DEBUG 24688 --- [nio-8080-exec-5] c.h.f.f.m.T.listEntity : <== Total: 2
回顾一下以前分页操作,最常用的一般都是一些分页工具。这里我也把分页工具方式写一下。
添加依赖
1 2 3 4 5 6 | <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> < dependency > < groupId >com.github.pagehelper</ groupId > < artifactId >pagehelper-spring-boot-starter</ artifactId > < version >1.4.0</ version > </ dependency > |
接口方法调整一下。
1 2 3 4 5 6 | @Override public PageInfo<TestFluentMybatisEntity> selectAllByPage2(PageReq pageReq) { PageHelper.startPage(pageReq.getPage(), pageReq.getSize()); return new PageInfo<>( testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery().selectAll())); } |
控制层代码。
1 2 3 4 5 6 7 8 9 10 | @ApiOperation (value = "分页查询所有数据2" , notes = "分页查询所有数据2" ) @RequestMapping (value = "/selectAllPage2" , method = RequestMethod.POST) @ResponseBody public Result<PageInfo<TestFluentMybatisEntity>> selectAllPage2( @RequestBody PageReq pageReq) { try { return Result.ok(selectService.selectAllByPage2(pageReq)); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null ); } } |
看下执行效果。
这里要注意一下,pageHelper的第一页是1,不是0,是有区别的。看下sql日志。
2021-10-25 11:27:37.135 DEBUG 24688 --- [nio-8080-exec-4] c.h.f.f.m.T.listEntity_COUNT : ==> Preparing: SELECT count(0) FROM `test_fluent_mybatis`
2021-10-25 11:27:37.136 DEBUG 24688 --- [nio-8080-exec-4] c.h.f.f.m.T.listEntity_COUNT : ==> Parameters:
2021-10-25 11:27:37.139 DEBUG 24688 --- [nio-8080-exec-4] c.h.f.f.m.T.listEntity_COUNT : <== Total: 1
2021-10-25 11:27:37.140 DEBUG 24688 --- [nio-8080-exec-4] c.h.f.f.m.T.listEntity : ==> Preparing: SELECT `id`, `age`, `create_time`, `del_flag`, `name` FROM `test_fluent_mybatis` LIMIT ?
2021-10-25 11:27:37.140 DEBUG 24688 --- [nio-8080-exec-4] c.h.f.f.m.T.listEntity : ==> Parameters: 2(Integer)
2021-10-25 11:27:37.142 DEBUG 24688 --- [nio-8080-exec-4] c.h.f.f.m.T.listEntity : <== Total: 2
是使用官方方式还是pageHelper,看习惯了,都可以。
如果本文对你有帮助,请点个赞支持一下吧。
到此这篇关于Java Fluent Mybatis 分页查询与sql日志输出详解流程篇的文章就介绍到这了,更多相关Java Fluent Mybatis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!