springboot整合mybatis分頁攔截器的問題小結(jié)
簡介
又到了吹水時(shí)間,是這樣的,今天開發(fā)時(shí)想將自己寫好的代碼拿來優(yōu)化,因?yàn)椴幌朐陂_發(fā)服弄,怕搞壞了到時(shí)候GIT到生產(chǎn)服一大堆問題,然后把它分離到我輪子(工具)項(xiàng)目上,最后運(yùn)行后發(fā)現(xiàn)我獲取List的時(shí)候很卡至少10秒,我驚了平時(shí)也就我的正常版本是800ms左右(不要看它很久,因?yàn)閿?shù)據(jù)量很大,也很正常。),前提是我也知道很慢,就等的確需要優(yōu)化時(shí),我在放出我優(yōu)化的plus版本,回到10秒哪里,最開始我剛剛接到這個(gè)app項(xiàng)目時(shí),在我用 PageHelper.startPage(page, num);(分頁),還沒等查到的數(shù)據(jù)封裝(PageInfo)就已經(jīng)分好頁了,現(xiàn)在換到輪子上就發(fā)現(xiàn)了這個(gè)問題,它沒有幫我將limit拼接到sql后面,導(dǎo)致我獲取全部,再PageInfo分頁,數(shù)據(jù)量龐大導(dǎo)致很卡,最后…
10秒:
正常的:
springboot整合mybatis分頁攔截器
分頁攔截實(shí)際上就是獲取sql后將sql拼接limit
pom.xml
<!-- 引入分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency>
yml
spring: application: name: spring-cloud-dynamic datasource: #類型 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/f2f?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: initial-size: 2 max-idle: 10 min-idle: 1 max-wait: 60000 max-active: 20 #最大空閑連接數(shù) #多久進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接 time-between-eviction-tuns-millis: 60000
MybatisConfig
/** * @author lanys * @Description: * @date 23/7/2021 下午8:38 */ @Configuration @EnableTransactionManagement @PropertySource(value = "classpath:application.yml", ignoreResourceNotFound = true) public class MybatisConfig implements TransactionManagementConfigurer { @Value("${mybatis.mapper-locations}") private String mapper; @Value("${mybatis.type-aliases-package}") private String aliases; @Autowired private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); // 設(shè)置數(shù)據(jù)源 bean.setDataSource(dataSource); // 設(shè)置xml bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper)); // 設(shè)置別名 bean.setTypeAliasesPackage(aliases); // 添加分頁插件 bean.setPlugins(new Interceptor[]{pageInterceptor()}); bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return bean.getObject(); } /** * 分頁攔截器 * @return */ private PageInterceptor pageInterceptor() { PageInterceptor pageInterceptor = new PageInterceptor(); // 詳見 com.github.pagehelper.page.PageParams Properties p = new Properties(); // RowBounds是否進(jìn)行count查詢 - 默認(rèn)不查詢 p.setProperty("rowBoundsWithCount", "true"); // 當(dāng)設(shè)置為true的時(shí)候,如果page size設(shè)置為0(或RowBounds的limit=0),就不執(zhí)行分頁,返回全部結(jié)果 p.setProperty("pageSizeZero", "true"); // 分頁合理化 p.setProperty("reasonable", "false"); // 是否支持接口參數(shù)來傳遞分頁參數(shù),默認(rèn)false p.setProperty("supportMethodsArguments", "true"); // 設(shè)置數(shù)據(jù)庫方言 , 也可以不設(shè)置,會動態(tài)獲取 p.setProperty("helperDialect", "mysql"); pageInterceptor.setProperties(p); return pageInterceptor; } @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }
測試
自己的其中代碼
/** * 關(guān)注列表 * @param userId * @param page * @param size * @return */ @Override public List<DynamicInfo> focusList(Long userId, Integer page, Integer size) { PageHelper.startPage(page, size); List<DynamicInfo> listByUserId = new ArrayList<>(); try { //獲取自己關(guān)注列表 listByUserId = this.dynamicReleaseMapper.getListFocusId(userId); if (listByUserId == null || listByUserId.size() == 0){ return listByUserId; } //List<DynamicInfo> listByUserId = this.dynamicReleaseMapper.getListFocusId(userId).stream().filter(x->(x.getIsPicture()!=2 && x.getIsVideo() !=2)||(x.getIsPicture()==2 && x.getIsVideo() !=2)||(x.getIsPicture()!=2 && x.getIsVideo() ==2)).collect(Collectors.toList()); publicGetDynamicInfo(userId,listByUserId); //} log.info("-------獲取關(guān)注列表-------"); return listByUserId; } catch (Exception e) { log.error("獲取關(guān)注列表異常",e); } return listByUserId; }
想分頁要加 PageHelper.startPage(page, size);,否則會默認(rèn)不分頁,也可以自己加limit.
結(jié)果(sql語句很長截取一部分):
GROUP BY id ORDER BY create_time desc LIMIT ?
總結(jié)
這代碼是最后發(fā)現(xiàn)不對后,問公司大佬才懂,瞬間學(xué)到了,可能有人會問,這有什么好處嗎?
優(yōu)點(diǎn)(自己的想法):
1. 直接在數(shù)據(jù)庫過濾,少了很多處理。為什么?因?yàn)槿绻阆炔槌鋈吭谶M(jìn)行封裝分頁,看不出什么問題。但是如果你直接獲取后,還要其他處理,比如一個(gè)動態(tài),你要獲取最新的動態(tài),動態(tài)需要圖片,視頻,標(biāo)簽,地址等,這一系列獲取,如果獲取到的動態(tài)很少還好,如果很多,那個(gè)速度自己都覺得可怕(慢)。
2. 運(yùn)行速度快很多,在開發(fā)中,盡量減少訪問數(shù)據(jù)庫來獲取數(shù)據(jù)。
3. 省事,有些是在sql后面自己加limit,但是sql語句很多時(shí),也很繁瑣
缺點(diǎn):
看個(gè)人需求,看合不合適
到此這篇關(guān)于springboot整合mybatis攔截器分頁的文章就介紹到這了,更多相關(guān)springboot整合mybatis分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決在微服務(wù)環(huán)境下遠(yuǎn)程調(diào)用feign和異步線程存在請求數(shù)據(jù)丟失問題
這篇文章主要介紹了解決在微服務(wù)環(huán)境下遠(yuǎn)程調(diào)用feign和異步線程存在請求數(shù)據(jù)丟失問題,主要包括無異步線程得情況下feign遠(yuǎn)程調(diào)用,異步情況下丟失上下文問題,需要的朋友可以參考下2022-05-05java -D參數(shù)設(shè)置系統(tǒng)屬性無效問題及解決
這篇文章主要介紹了java -D參數(shù)設(shè)置系統(tǒng)屬性無效問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Java實(shí)戰(zhàn)角色權(quán)限后臺腳手架系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實(shí)現(xiàn)一個(gè)角色權(quán)限后臺腳手架系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01