日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot中的mybatis是如果使用pagehelper的

發(fā)布時間:2024/9/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot中的mybatis是如果使用pagehelper的 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

springboot中的mybatis是如果使用pagehelper的

springboot中使用其他組件都是基于自動配置的AutoConfiguration配置累的,pagehelper插件也是一樣的,通過PageHelperAutoConfiguration的,這個類存在于jar包的spring.factories

文件中,當(dāng)springboot啟動時會通過selector自動將配置類加載到上下文中

springboot集成pagehelper,pom依賴:

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.2</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-autoconfigure</artifactId> ---作用自動配置pagehelper<version>1.2.3</version></dependency>

?

關(guān)鍵類:

PageHelperAutoConfiguration.java

package com.github.pagehelper.autoconfigure;import com.github.pagehelper.PageInterceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct; import java.util.List; import java.util.Properties;/*** 自定注入分頁插件** @author liuzh*/ @Configuration //表明當(dāng)前了是一個配置類 @ConditionalOnBean(SqlSessionFactory.class)//當(dāng)有SqlSesionFactory這個類的時候才實(shí)例化當(dāng)前類 @EnableConfigurationProperties(PageHelperProperties.class)//pagehelpser的配置文件 @AutoConfigureAfter(MybatisAutoConfiguration.class)//當(dāng)前類是在mybatisAutoconfiguration配置類初始化之后才初始化 public class PageHelperAutoConfiguration {@Autowiredprivate List<SqlSessionFactory> sqlSessionFactoryList;@Autowiredprivate PageHelperProperties properties;//配置文件/*** 接受分頁插件額外的屬性** @return*/@Bean@ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX)//將配置文件實(shí)例化為propertiespublic Properties pageHelperProperties() {return new Properties();}@PostConstruct//實(shí)例化之后條用當(dāng)前方法,將pagehelper插件添加到myabtis的核心配置文件中(Configuration中) PageInterceptor就是pagehelper的插件public void addPageInterceptor() {PageInterceptor interceptor = new PageInterceptor();Properties properties = new Properties();//先把一般方式配置的屬性放進(jìn)去properties.putAll(pageHelperProperties());//在把特殊配置放進(jìn)去,由于close-conn 利用上面方式時,屬性名就是 close-conn 而不是 closeConn,所以需要額外的一步properties.putAll(this.properties.getProperties());interceptor.setProperties(properties);for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {sqlSessionFactory.getConfiguration().addInterceptor(interceptor);}}}

進(jìn)一步看PageInterceptor.java,這個類就是mybatis的插件,先看類上的注解,@Interceptos注解表名當(dāng)前類是一個攔截器,里邊會有@Signature注解,這個注解主要配置的就是攔截的方法,如type:攔截的類是Executor,method:攔截的方法,args:攔截方法

的參數(shù)

@Intercepts(//這個注解是mybatis中的注解{@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),} ) public class PageInterceptor implements Interceptor {//緩存count查詢的msprotected Cache<String, MappedStatement> msCountMap = null;private Dialect dialect;//這個是pagehelper的一個接口,里邊有數(shù)據(jù)庫的一些方言配置private String default_dialect_class = "com.github.pagehelper.PageHelper";//默認(rèn)的方言實(shí)現(xiàn)類,如果上邊那個dialect沒有配置的話,默認(rèn)走這個private Field additionalParametersField;private String countSuffix = "_COUNT";

Dialect的一些實(shí)現(xiàn)類,不同的數(shù)據(jù)庫使用不同的實(shí)現(xiàn)類,其中,pagehelper是默認(rèn)的方言(這個類是在沒有配置其他方言的時候,默認(rèn)實(shí)例化的)

PageInterceptor類中的關(guān)鍵方法:intercept攔截方法,當(dāng)攔截到相應(yīng)的方法后,后走該改法判斷時候需要改變

@Overridepublic Object intercept(Invocation invocation) throws Throwable {try {Object[] args = invocation.getArgs();//獲取攔截到的方法的所有參數(shù) 下邊獲取0,1,2,3是進(jìn)行強(qiáng)制轉(zhuǎn)換對應(yīng)的類,這個是根據(jù)@intercepts注解中的@sisignature中的args得到的MappedStatement ms = (MappedStatement) args[0];Object parameter = args[1];RowBounds rowBounds = (RowBounds) args[2];ResultHandler resultHandler = (ResultHandler) args[3];Executor executor = (Executor) invocation.getTarget();CacheKey cacheKey;BoundSql boundSql;//由于邏輯關(guān)系,只會進(jìn)入一次 分兩種情況4個參數(shù)和6個參數(shù)也是根據(jù)注解@signature的args類判斷的if(args.length == 4){//4 個參數(shù)時boundSql = ms.getBoundSql(parameter);cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);} else {//6 個參數(shù)時cacheKey = (CacheKey) args[4];boundSql = (BoundSql) args[5];}List resultList;//調(diào)用方法判斷是否需要進(jìn)行分頁,如果不需要,直接返回結(jié)果 例如dialect使用的是默認(rèn)的實(shí)現(xiàn)類的話,判斷是否要跳過分頁,是根據(jù)代碼中查詢數(shù)據(jù)庫是,是否執(zhí)行過PageHelper.startPage()方法類判斷的(一種情況),執(zhí)行過,則要分頁,否則不分頁if (!dialect.skip(ms, parameter, rowBounds)) {//反射獲取動態(tài)參數(shù)String msId = ms.getId();Configuration configuration = ms.getConfiguration();Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);//下邊會進(jìn)行兩步:1、總數(shù)查詢 2、分頁查詢//判斷是否需要進(jìn)行 count 查詢if (dialect.beforeCount(ms, parameter, rowBounds)) {String countMsId = msId + countSuffix;Long count;//先判斷是否存在手寫的 count 查詢MappedStatement countMs = getExistedMappedStatement(configuration, countMsId);if(countMs != null){count = executeManualCount(executor, countMs, parameter, boundSql, resultHandler);} else {countMs = msCountMap.get(countMsId);//自動創(chuàng)建if (countMs == null) {//根據(jù)當(dāng)前的 ms 創(chuàng)建一個返回值為 Long 類型的 mscountMs = MSUtils.newCountMappedStatement(ms, countMsId);msCountMap.put(countMsId, countMs);}count = executeAutoCount(executor, countMs, parameter, boundSql, rowBounds, resultHandler);}//處理查詢總數(shù)//返回 true 時繼續(xù)分頁查詢,false 時直接返回if (!dialect.afterCount(count, parameter, rowBounds)) {//當(dāng)查詢總數(shù)為 0 時,直接返回空的結(jié)果return dialect.afterPage(new ArrayList(), parameter, rowBounds);}}//判斷是否需要進(jìn)行分頁查詢if (dialect.beforePage(ms, parameter, rowBounds)) {//生成分頁的緩存 keyCacheKey pageKey = cacheKey;//處理參數(shù)對象parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);//調(diào)用方言獲取分頁 sqlString pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);BoundSql pageBoundSql = new BoundSql(configuration, pageSql, boundSql.getParameterMappings(), parameter);//設(shè)置動態(tài)參數(shù)for (String key : additionalParameters.keySet()) {pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));}//執(zhí)行分頁查詢resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);} else {//不執(zhí)行分頁的情況下,也不執(zhí)行內(nèi)存分頁resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);}} else {//rowBounds用參數(shù)值,不使用分頁插件處理時,仍然支持默認(rèn)的內(nèi)存分頁resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);}return dialect.afterPage(resultList, parameter, rowBounds);} finally {dialect.afterAll();}}

?

例子:

Page<TUser> startPage = PageHelper.startPage(1, 2);List<TUser> list1 = mapper.selectByEmailAndSex1(params);

?//在查詢數(shù)據(jù)庫之前先執(zhí)行startPage方法,傳入分頁參數(shù)進(jìn)入starepage方法(這個方法在pagehelper的父類pagemethod中),startPage方法有多個重載的方法,該例子中用的是兩個參數(shù)的

/*** 開始分頁** @param pageNum 頁碼* @param pageSize 每頁顯示數(shù)量*/public static <E> Page<E> startPage(int pageNum, int pageSize) {return startPage(pageNum, pageSize, true);} /*** 開始分頁** @param pageNum 頁碼* @param pageSize 每頁顯示數(shù)量* @param count 是否進(jìn)行count查詢*/public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count) {return startPage(pageNum, pageSize, count, null, null);} /*** 開始分頁** @param pageNum 頁碼* @param pageSize 每頁顯示數(shù)量* @param count 是否進(jìn)行count查詢* @param reasonable 分頁合理化,null時用默認(rèn)配置* @param pageSizeZero true且pageSize=0時返回全部結(jié)果,false時分頁,null時用默認(rèn)配置*/public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {Page<E> page = new Page<E>(pageNum, pageSize, count);page.setReasonable(reasonable);page.setPageSizeZero(pageSizeZero);//當(dāng)已經(jīng)執(zhí)行過orderBy的時候Page<E> oldPage = getLocalPage();//查詢ThreadLocal中是否已經(jīng)有了pageif (oldPage != null && oldPage.isOrderByOnly()) {page.setOrderBy(oldPage.getOrderBy());}setLocalPage(page);return page;}

?

posted @ 2019-05-28 22:40 巡山小妖N 閱讀(...) 評論(...) 編輯 收藏

總結(jié)

以上是生活随笔為你收集整理的springboot中的mybatis是如果使用pagehelper的的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。