1. 程式人生 > >springboot + mybatis配置分頁插件

springboot + mybatis配置分頁插件

XML control framework cti sarg location @override html apache

1:首先配置springboot +mybatis框架 參考:http://www.cnblogs.com/liyafei/p/7911549.html

2:創建配置類MybatisConfig,對分頁插件進行配置。將mybatis-config.xml移動到classpath路徑下。

  

package com.liyafei.util.pagehelper;

import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer; import com.github.pagehelper.PageHelper; @Configuration //添加這個註解相當於配置文件 @EnableTransactionManagement public class MyBatisConfig implements TransactionManagementConfigurer { //MybatisConfig將會映射到classpath下的mybaits-config.xml,功能和xml下面配置類似 @Autowired DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //分頁插件 PageHelper pageHelper = new PageHelper(); Properties props = new Properties(); props.setProperty("reasonable", "true"); props.setProperty("supportMethodsArguments", "true"); props.setProperty("returnPageInfo", "check"); props.setProperty("params", "count=countSql"); pageHelper.setProperties(props); //添加插件 bean.setPlugins(new Interceptor[]{pageHelper}); try { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); return null; } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }

3:在controller中使用分頁插件pagehelper,分頁插件只能配置一個,不能多用。調用PageHelper.startPage之後下一行(只作用於這一行)的查詢結果將會自動調用分頁插件
  

    @RequestMapping("/query/{page}/{pageSize}")
    public PageInfo query(@PathVariable Integer page, @PathVariable Integer pageSize) {
        if(page!= null && pageSize!= null){
            System.out.println("page"+page+"pageSize"+pageSize);
            PageHelper.startPage(page, pageSize);
        }
        List<User> userList = userService.getUserList();
        
        for(User user:userList){
            System.out.print(user.getId());
            System.out.println(user.getUsername());
            
        }
        
        return new PageInfo(userList);
    }


4:測試成功

  

技術分享圖片

springboot + mybatis配置分頁插件