Spring Boot 配置 Mybatis 通用 Mapper 和 pagehelper 分页插件
一、POM.XML 添加 tk.mybatis , pagehelper 依赖
<!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.1</version> </dependency> <!--通用Mapper插件--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.3</version> </dependency>二、application.properties 配置Mybatis , PageHelper
mybatis.config-location=classpath:mybatis-config.xml mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml mybatis.type-aliases-package=cx.xix.wechatprogram.model mapper.mappers=tk.mybatis.mapper.common.MySqlMapper mapper.not-empty=false mapper.identity=MYSQL pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params.count=countSql
三、Spring Boot 形式配置 替代了XML配置
1. 在Spring Boot 启动类 同级package下新建 MybatisProperties.java
import org.apache.ibatis.session.ExecutorType; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.io.Resource; @ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX) public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; /** * Config file path. */ private String config; /** * Location of mybatis mapper files. */ private Resource[] mapperLocations; /** * Package to scan domain objects. */ private String typeAliasesPackage; /** * Package to scan handlers. */ private String typeHandlersPackage; /** * Check the config file exists. */ private boolean checkConfigLocation = false; /** * Execution mode. */ private ExecutorType executorType = ExecutorType.SIMPLE; public String getConfig() { return this.config; } public void setConfig(String config) { this.config = config; } public Resource[] getMapperLocations() { return this.mapperLocations; } public void setMapperLocations(Resource[] mapperLocations) { this.mapperLocations = mapperLocations; } public String getTypeHandlersPackage() { return this.typeHandlersPackage; } public void setTypeHandlersPackage(String typeHandlersPackage) { this.typeHandlersPackage = typeHandlersPackage; } public String getTypeAliasesPackage() { return this.typeAliasesPackage; } public void setTypeAliasesPackage(String typeAliasesPackage) { this.typeAliasesPackage = typeAliasesPackage; } public boolean isCheckConfigLocation() { return this.checkConfigLocation; } public void setCheckConfigLocation(boolean checkConfigLocation) { this.checkConfigLocation = checkConfigLocation; } public ExecutorType getExecutorType() { return this.executorType; } public void setExecutorType(ExecutorType executorType) { this.executorType = executorType; } } 2. 在Spring Boot 启动类 同级package下新建 MybatisConfigurer.java import com.github.pagehelper.PageHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; 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 tk.mybatis.spring.mapper.MapperScannerConfigurer; import javax.annotation.Resource; import javax.sql.DataSource; import java.util.Properties; @Configuration @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class }) @EnableConfigurationProperties(MybatisProperties.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class) public class MybatisConfigurer { private static Log log = LogFactory.getLog(MybatisConfigurer.class); @Resource private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("实体类所属包"); //分页插件 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("reasonable", "true"); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("returnPageInfo", "check"); properties.setProperty("params", "count=countSql"); pageHelper.setProperties(properties); //添加插件 bean.setPlugins(new Interceptor[]{pageHelper}); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); bean.setMapperLocations(resolver.getResources("mapper.xml路径")); return bean.getObject(); } /** * 分页插件 * * @param dataSource * @return * @author SHANHY * @create 2016年2月18日 */ @Bean public PageHelper pageHelper(DataSource dataSource) { log.info("注册MyBatis分页插件PageHelper"); PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } @Configuration @AutoConfigureAfter(MybatisConfigurer.class) public static class MyBatisMapperScannerConfigurer { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); mapperScannerConfigurer.setBasePackage("Dao路径"); //配置通用mappers Properties properties = new Properties(); properties.setProperty("mappers", "包名.MapperDao"); properties.setProperty("notEmpty", "false"); properties.setProperty("STDOUT_LOGGING", "logImpl"); properties.setProperty("IDENTITY", "MYSQL"); properties.setProperty("ORDER", "BEFORE"); mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; } } }四、新增 MapperDao.java 通用Dao类
import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; public interface MapperDao<T> extends Mapper<T>,MySqlMapper<T>{ }注意:MapperDao 不能被Spring Scan 扫描到
五、 配置启动类:
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("cx.xix.wechatprogram.dao")//不可扫描到MapperDao.java
public class WechatprogramApplication extends SpringBootServletInitializer implements WebApplicationInitializer{
public static void main(String[] args) {
SpringApplication.run(WechatprogramApplication.class, args);
}}
六、配置过程中遇到启动的错误:
若启动遇到类似错误,一般都是因为 存放mapper.xml的路径下没有 xml 文件导致 新增一个即可
遇到 sqlSessionFactoryBean 错误 修改MybatisConfigurer.java
中的 public SqlSessionFactory sqlSessionFactoryBean() 方法名称 即可
成功启动 log 如下:
配置成功~ 祝您撸代码愉快~
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- Spring Boot : 集成mybatis、pagehelper插件、开启事务、druid数据源配置、mybatis-generator(十一)
- springboot+Mybatis整合pageHelper分页插件
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- springboot 集成myBatis PageHelper 分页插件
- spring-boot 集成mybatis的分页插件PageHelper和Generator
- springboot2.0.5集成mybatis(PageHelper分页插件、generator插件使用)
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- Spring,SpringBoot 集成 MyBatis 的分页插件 PageHelper
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)
- Mybatis分页插件 - PageHelper与springBoot的整合
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- spring-boot 集成mybatis的分页插件PageHelper版本问题
- SpringBoot2.0学习之集成MyBatis PageHelper分页插件
- SpringBoot Mybatis PageHelper分页插件的两种用法(一)
- SpringBoot集成MyBatis、PageHelper和通用Mapper
- SpringBoot_thymeleaf_mybatis和PageHelper分页插件整合
- springboot如何集成mybatis的pagehelper分页插件
- spring-boot | 整合通用Mabatis 分页插件PageHelper
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页