SpringBoot集成MyBatis的分页插件PageHelper

  昨天总结了本人学习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

原文地址:https://www.cnblogs.com/qlqwjy/p/8552078.html