Java Fluent Mybatis 分頁查詢與sql日志輸出詳解流程篇
前言
接著我上一章:Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 下
上一章我把項(xiàng)目做了一部分工程化包裝,主要還是想要之后的調(diào)試能夠方便一些。那么這一章接著上一章的內(nèi)容,做一下查詢分頁,并且將每次請(qǐng)求所調(diào)用的sql語句寫在日志里面,便于我們觀察定位問題。代碼之后還是會(huì)上傳到github。
GitHub代碼倉庫地址:GitHub倉庫
準(zhǔn)備數(shù)據(jù)
簡(jiǎn)單的準(zhǔn)備了一些數(shù)據(jù)。

Sql日志配置
之前sql日志一直沒有配置,后面隨著使用的語句愈發(fā)復(fù)雜,決定先把日志配置上。方便調(diào)試。
這部分和fm沒什么關(guān)系,如果你會(huì)配置的話,可以跳到下個(gè)標(biāo)題。這里把logback.xml發(fā)出來
<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>
簡(jiǎn)單說明一下,主要是下面這部分的配置,將mapper包內(nèi)的日志打出來。
<logger name="com.hy.fmp.fluent.mapper" level="debug">
<appender-ref ref="SQL"/>
</logger>
官方分頁查詢
我這里先按照官方的分頁查詢使用編寫接口。官方提供的寫法是Query語句中有l(wèi)imit方法,和mysql的limit關(guān)鍵字一致。
先定義分頁實(shí)體
import lombok.Data;
/**
* @Program: fluent-mybatis-project @ClassName: PageReq @Author: huyi @Date: 2021-10-24
* 23:21 @Description: 分頁參數(shù) @Version: V1.0
*/
@Data
public class PageReq {
/** 每頁數(shù)量 */
private Integer size;
/** 頁碼 */
private Integer page;
}
這里注意,我們要按照limit的語法來,下面是接口方法實(shí)現(xiàn)。
參數(shù)1:指定要返回的第一行的偏移量。第一行的偏移量為0,而不是1,所以對(duì)應(yīng)我們的參數(shù)就是
pageReq.getPage() * pageReq.getSize()
參數(shù)2:指定要返回的最大行數(shù)。
pageReq.getSize()
@Autowired private TestFluentMybatisMapper testFluentMybatisMapper;
@Override
public StdPagedList<TestFluentMybatisEntity> selectAllByPage(PageReq pageReq) {
return testFluentMybatisMapper.stdPagedEntity(
new TestFluentMybatisQuery()
.selectAll()
.limit(pageReq.getPage() * pageReq.getSize(), pageReq.getSize()));
}
控制層:
@Autowired private ISelectService selectService;
@ApiOperation(value = "分頁查詢所有數(shù)據(jù)", notes = "分頁查詢所有數(shù)據(jù)")
@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);
}
}
看下執(zhí)行效果

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
PageHelper分頁查詢
回顧一下以前分頁操作,最常用的一般都是一些分頁工具。這里我也把分頁工具方式寫一下。
添加依賴
<!-- 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>
接口方法調(diào)整一下。
@Override
public PageInfo<TestFluentMybatisEntity> selectAllByPage2(PageReq pageReq) {
PageHelper.startPage(pageReq.getPage(), pageReq.getSize());
return new PageInfo<>(
testFluentMybatisMapper.listEntity(new TestFluentMybatisQuery().selectAll()));
}
控制層代碼。
@ApiOperation(value = "分頁查詢所有數(shù)據(jù)2", notes = "分頁查詢所有數(shù)據(jù)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);
}
}
看下執(zhí)行效果。

這里要注意一下,pageHelper的第一頁是1,不是0,是有區(qū)別的??聪聅ql日志。
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
總結(jié)
是使用官方方式還是pageHelper,看習(xí)慣了,都可以。
如果本文對(duì)你有幫助,請(qǐng)點(diǎn)個(gè)贊支持一下吧。

到此這篇關(guān)于Java Fluent Mybatis 分頁查詢與sql日志輸出詳解流程篇的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)模式詳解之門面模式(外觀模式)
為子系統(tǒng)中的一組接口提供一個(gè)一致的界面, Facade 模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。本文給大家介紹Java設(shè)計(jì)模式詳解之門面模式(外觀模式),感興趣的朋友參考下吧2016-04-04
Java中實(shí)現(xiàn)WebSocket方法詳解
這篇文章主要介紹了Java中實(shí)現(xiàn)WebSocket方法詳解,WebSocket?是一種新型的網(wǎng)絡(luò)協(xié)議,它允許客戶端和服務(wù)器之間進(jìn)行雙向通信,可以實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)交互,需要的朋友可以參考下2023-07-07
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)項(xiàng)目之寵物商城系統(tǒng)的實(shí)現(xiàn)流程
這是一個(gè)使用了java+Springboot+Maven+mybatis+Vue+mysql開發(fā)的寵物商城系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有寵物商城該有的所有功能,感興趣的朋友快來看看吧2022-01-01
MyBatis完成CRUD?詳細(xì)細(xì)節(jié)內(nèi)容剖析
這篇文章主要介紹了MyBatis完成CRUD?詳細(xì)細(xì)節(jié)內(nèi)容剖析,本文通過圖文示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-05-05
Spring?AOP?后置通知修改響應(yīng)httpstatus方式
這篇文章主要介紹了Spring?AOP?后置通知修改響應(yīng)httpstatus方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
java自定義ClassLoader加載指定的class文件操作
這篇文章主要介紹了java自定義ClassLoader加載指定的class文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02

