您的位置:首页 > 编程语言 > Java开发

Spring Boot 配置 Mybatis 通用 Mapper 和 pagehelper 分页插件

2018-04-12 17:59 453 查看

一、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 如下:



配置成功~ 祝您撸代码愉快~



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: