1. 程式人生 > >SpringBoot集成MyBatis的分頁插件PageHelper

SpringBoot集成MyBatis的分頁插件PageHelper

今天 作用 png 啟動 定義 col typealias 整合 目錄

  昨天總結了本人學習springboot整合mybatis第一階段的一些學習心得和源碼,主要就算是敲了一下SpringBoot的門兒,希望能給各位的入門帶給一點兒捷徑,今天給各位溫習一下MyBatis的分頁插件PageHelper和SpringBoot的集成,它的使用也非常簡單,開發更為高效。因為PageHelper插件是屬於MyBatis框架的。

0.目錄結構:

技術分享圖片

1.pom.xml引入jar包:

        <dependency>
            <groupId>com.github.pagehelper</groupId
> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>

----------------------第一種整合方式(修改配置文件)---------------------------------------

2.修改Mybatis主配置文件,加上插件:(註意property的name用helperDialect,與spring整合必須用helperDialect,用dialect會報錯

)

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- <settings> 開啟二級緩存 <setting name="cacheEnabled" value="true" /> </settings> 
--> <!-- 只需要定義個別名,這個應該有 --> <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 使用下面的方式配置參數,後面會有所有的參數介紹 --> <property name="helperDialect" value="mysql" /> </plugin> </plugins> </configuration>

3.MyBatisConfig .java (mybatis配置類)

package cn.qlq.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.github.pagehelper.PageInterceptor;

@Configuration
public class MyBatisConfig {

    @Bean
    @ConditionalOnMissingBean // 當容器裏沒有指定的Bean的情況下創建該對象
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 設置數據源
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 設置mybatis的主配置文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/SqlMapConfig.xml");
        sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
        // 設置別名包
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.qlq.bean");
        
        return sqlSessionFactoryBean;
    }
    
    
    
    
}

4.測試:

    @RequestMapping("list")
    public List<User> list(){
         //只對緊鄰的下一條select語句進行分頁查詢,對之後的select不起作用
        PageHelper.startPage(1,3);
        List<User> list = userService.findAllUser();
        return list;
    }

4.啟動測試:

技術分享圖片

----------------------第二種整合方式(修改配置類)---------------------------------------

  • SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- <settings> 開啟二級緩存 <setting name="cacheEnabled" value="true" /> </settings> -->
    <!-- 只需要定義個別名,這個應該有 -->
</configuration>

  • MyBatisConfig .java (mybatis配置類 參數也要用helperDialect,否則會報錯)
package cn.qlq.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.github.pagehelper.PageInterceptor;

@Configuration
public class MyBatisConfig {

    @Bean
    @ConditionalOnMissingBean // 當容器裏沒有指定的Bean的情況下創建該對象
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 設置數據源
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 設置mybatis的主配置文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/SqlMapConfig.xml");
        sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
        // 設置別名包
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.qlq.bean");
        
        
        //分頁插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("reasonable", "true");
        properties.setProperty("params", "pageNum=pageNum;pageSize=pageSize");
        pageInterceptor.setProperties(properties);
        
        //添加插件
       sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor});
        
        return sqlSessionFactoryBean;
    }
    
    
    
    
}

  • 啟動測試

技術分享圖片

pageHelper使用以及未集成springboot配置參考:  http://www.cnblogs.com/qlqwjy/p/8442148.html

SpringBoot集成MyBatis的分頁插件PageHelper