直接上代码
package com.pbh.springbootdemo.config.mybatis;
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInterceptor;
import com.pbh.springbootdemo.config.datasource.DataBaseConfiguration;
import com.pbh.springbootdemo.config.datasource.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
//import java.io.IOException;
//import java.util.Properties;
/**
* @ClassName MybatisConfiguration
* @Descrition MyBatis的配置类
* MybatisConfiguration 主要是配置的sqlSessionFactory和sqlSessionTemplate,以及Mybatis的扩展框架Mapper的配置,如果不需要Mapper,可以不用配置scannerConfigurer
* @Author Pbh
* @Date 2018/9/3 13:04
* @Version 1.0
**/
@Configuration
@AutoConfigureAfter({ DataBaseConfiguration.class })
@Slf4j
public class MybatisConfiguration {
@Bean(name = "sqlSessionFactory")
@Autowired
public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource);
//因为new了一个SqlSessionFactoryBean 所以yml的mapper-locations的配置没有用,需要手动配置扫描的包
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapping/*.xml"));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//分页插件设置(pom.xml 引入了pagehelper-spring-boot-starter 再这里再配置 会配置重复报错)
/*bean.setTypeAliasesPackage("com.pbh.springbootdemo.model.*");//设置扫描的实体包
//PageHelper pageHelper = new PageHelper();
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
//pageHelper.setProperties(properties);
pageInterceptor.setProperties(properties);
//添加分页插件到ibatis拦截器
bean.setPlugins(new Interceptor[]{pageInterceptor});*/
//配置通用Mapper
try {
SqlSessionFactory session = bean.getObject();
MapperHelper mapperHelper = new MapperHelper();
//特殊配置
Config config = new Config();
//具体支持的参数看后面的文档
config.setNotEmpty(true);
//设置配置
mapperHelper.setConfig(config);
// 注册自己项目中使用的通用Mapper接口,这里没有默认值,必须手动注册
mapperHelper.registerMapper(Mapper.class);
//配置完成后,执行下面的操作
mapperHelper.processConfiguration(session.getConfiguration());
return session;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean(name = "sqlSessionTemplate")
@Autowired
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public MapperScannerConfigurer scannerConfigurer(){
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
configurer.setSqlSessionTemplateBeanName("sqlSessionTemplate");
configurer.setBasePackage("com.pbh.springbootdemo.mapper.*");
configurer.setMarkerInterface(Mapper.class);
return configurer;
}
}
主要在这段
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapping/*.xml"));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
后来在tk的gitee上找到了相关文档
https://gitee.com/free/Mapper/wikis/1.3-spring-boot?sort_id=208198
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>版本号</version>
</dependency>
注意:引入该 starter 时,和 MyBatis 官方的 starter 没有冲突,但是官方的自动配置不会生效!