spring boot和mybatis集成分页插件
2017-04-24 15:57
826 查看
MyBatis提供了拦截器接口,我们可以实现自己的拦截器,将其作为一个plugin装入到SqlSessionFactory中。
首先要说的是,Spring在依赖注入bean的时候,会把所有实现MyBatis中Interceptor接口的所有类都注入到SqlSessionFactory中,作为plugin存在。既然如此,我们集成一个plugin便很简单了,只需要使用@Bean创建PageHelper对象即可。
1、添加pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
2、MyBatisConfiguration.java类配置
package com.example.mybatis;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
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.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 {
@Autowired
private DataSource dataSource;
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(PageHelper pageHelper) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//自定义数据库配置的时候,需要将pageHelper的bean注入到Plugins中,如果采用系统默认的数据库配置,则只需要定义pageHelper的bean,会自动注入。
bean.setPlugins(new Interceptor[] { pageHelper });
try {
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
p.setProperty("dialect", "mysql");
pageHelper.setProperties(p);
return pageHelper;
}
}
3、分页查询测试
@RequestMapping("/likename")
public List<Student> likeName(@RequestParam String name){
PageHelper.startPage(1, 1);
return stuMapper.likeName(name);
}
首先要说的是,Spring在依赖注入bean的时候,会把所有实现MyBatis中Interceptor接口的所有类都注入到SqlSessionFactory中,作为plugin存在。既然如此,我们集成一个plugin便很简单了,只需要使用@Bean创建PageHelper对象即可。
1、添加pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
2、MyBatisConfiguration.java类配置
package com.example.mybatis;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
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.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 {
@Autowired
private DataSource dataSource;
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(PageHelper pageHelper) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//自定义数据库配置的时候,需要将pageHelper的bean注入到Plugins中,如果采用系统默认的数据库配置,则只需要定义pageHelper的bean,会自动注入。
bean.setPlugins(new Interceptor[] { pageHelper });
try {
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
p.setProperty("dialect", "mysql");
pageHelper.setProperties(p);
return pageHelper;
}
}
3、分页查询测试
@RequestMapping("/likename")
public List<Student> likeName(@RequestParam String name){
PageHelper.startPage(1, 1);
return stuMapper.likeName(name);
}
相关文章推荐
- SpringBoot学习笔记之mybatis分页插件集成
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- SpringBoot集成MyBatis的分页插件PageHelper
- spring boot和mybatis集成分页插件
- spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 集成通用maper,与分页插件:
- spring-boot 集成mybatis的分页插件PageHelper和Generator
- springboot如何集成mybatis的pagehelper分页插件
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- spring boot和mybatis集成分页插件
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- springboot的mybatis集成分页插件PageHelper
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页
- Intellij IDEA Spring Boot (10) 集成mybatis以及分页
- Mybatis分页插件 - PageHelper与springBoot的整合
- Mybatis-分页插件(spring boot下)
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)