Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does springboot integrate mybatis paging interceptor

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how springboot integrates mybatis paging interceptor". In daily operation, I believe that many people have doubts about how springboot integrates mybatis paging interceptor. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how springboot integrates mybatis paging interceptor". Next, please follow the editor to study!

Brief introduction

Today, when developing, I want to optimize the code I wrote, because I don't want to mess with the development server, for fear of damaging the GIT to the production service, and then separate it into my wheel (tool) project. Finally, after running it, I found that when I got List, it was very stuck for at least 10 seconds. I was surprised that my normal version is about 800ms (don't look at it for a long time, because the amount of data is very large and normal.) The premise is that I also know that it is slow, and when I do need optimization, I am releasing my optimized plus version, back to where in 10 seconds, when I first received this app project, when I used PageHelper.startPage (page, num) (paging), the data encapsulation (PageInfo) has already been divided before it is found. Now I found this problem on the wheel. It did not help me to connect the limit to the back of the sql, which led me to get all of it, and then PageInfo paging. The huge amount of data led to very stuck. Finally...

10 seconds:

Normal:

Springboot integrates mybatis paging interceptor

Paging interception is actually getting sql and splicing sql into limit.

Pom.xml

Com.github.pagehelper pagehelper 5.2.0 mysql mysql-connector-java 8.0.21 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 com.alibaba druid

Yml

Spring: application: name: spring-cloud-dynamic datasource: # Type 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 # maximum number of idle connections # how often is it checked Detect idle connections that need to be closed time-between-eviction-tuns-millis: 60000

MybatisConfig

/ * @ author lanys * @ Description: * @ date 23 * / @ 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 (); / / set data source bean.setDataSource (dataSource); / / set xml bean.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources (mapper)); / / set alias bean.setTypeAliasesPackage (aliases) / / add the paging plug-in bean.setPlugins (new Interceptor [] {pageInterceptor ()}); bean.getObject () .getConfiguration () .setMapUnderscoreToCamelCase (true); return bean.getObject ();} / * paging interceptor * @ return * / private PageInterceptor pageInterceptor () {PageInterceptor pageInterceptor = new PageInterceptor () / / for more information, please see com.github.pagehelper.page.PageParams Properties p = new Properties (); / / whether RowBounds does count query-p.setProperty ("rowBoundsWithCount", "true") is not queried by default; / / when set to true, if page size is set to 0 (or RowBounds's limit=0), paging is not performed and all results p.setProperty ("pageSizeZero", "true") are returned. / / paging rationalization p.setProperty ("reasonable", "false"); / / whether API parameters are supported to pass paging parameters. Default false p.setProperty ("supportMethodsArguments", "true"); / / set database dialect, or not, and dynamically get p.setProperty ("helperDialect", "mysql"); pageInterceptor.setProperties (p) Return pageInterceptor;} @ Override public PlatformTransactionManager annotationDrivenTransactionManager () {return new DataSourceTransactionManager (dataSource);}}

test

Your own code in it.

/ * param userId * @ param page * @ param size * @ return * / @ Override public List focusList (Long userId, Integer page, Integer size) {PageHelper.startPage (page, size); List listByUserId = new ArrayList (); try {/ / get your own follow list listByUserId = this.dynamicReleaseMapper.getListFocusId (userId) If (listByUserId = = null | | listByUserId.size () = = 0) {return listByUserId } / / List 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)) .x.getIsVideo ()); publicGetDynamicInfo (userId,listByUserId) / /} log.info ("- get watch list -"); return listByUserId;} catch (Exception e) {log.error ("get watch list exception", e);} return listByUserId;}

Add PageHelper.startPage (page, size) if you want to page, otherwise it will not be paged by default, or you can add your own limit.

Result (the sql statement is very long to intercept part of it):

GROUP BY id ORDER BY create_time desc LIMIT? At this point, the study on "how springboot integrates the mybatis paging interceptor" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report