目录
pom包引入:略
yml文件配置:
代码配置:
1.Mapper基类创建:
2.数据源配置:
3.基类包扫描
使用基类继承方式实现多数据源,根据继承的接口动态切换数据源
-
pom包引入:略
-
yml文件配置:
spring:
datasource:
entry:
jdbc-url: jdbc:mysql://xxx:3306/wiki_entry?characterEncoding=UTF-8&useSSL=false
username: xxx
password: xxx
video:
jdbc-url: jdbc:mysql://xxx:3306/wiki_video?characterEncoding=UTF-8&useSSL=false
username: xxx
password: xxx
-
代码配置:
1.Mapper基类创建:
public interface EntryMapper {
}
public interface VideoMapper {
}
2.数据源配置:
@Configuration
public class EntryDataSourceConfig {
@Autowired
private MybatisInterceptor mybatisInterceptor;
@ConfigurationProperties(prefix = "spring.datasource.entry")
@Bean(name = "entryDataSource")
@Primary
public DataSource entryDataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entrySqlSessionFactory")
public SqlSessionFactory entrySqlSessionFactory(@Qualifier("entryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//设置分页拦截器
sqlSessionFactoryBean.setPlugins(new Interceptor[]{mybatisInterceptor});
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
//驼峰配置
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
sqlSessionFactory.getConfiguration().setLogImpl(MybatisSqlCustomLog.class);
return sqlSessionFactory;
}
@Primary
@Bean
public DataSourceTransactionManager entryDataSourceTransactionManager(@Qualifier("entryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean
public SqlSessionTemplate entrySqlSessionTemplate(@Qualifier("entrySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Configuration
public class VideoDataSourceConfig {
@Autowired
private MybatisInterceptor mybatisInterceptor;
@ConfigurationProperties(prefix = "spring.datasource.video")
@Bean(name = "videoDataSource")
public DataSource videoDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "videoSqlSessionFactory")
public SqlSessionFactory videoSqlSessionFactory(@Qualifier("videoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//设置分页拦截器
sqlSessionFactoryBean.setPlugins(new Interceptor[]{mybatisInterceptor});
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
//驼峰配置
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
sqlSessionFactory.getConfiguration().setLogImpl(MybatisSqlCustomLog.class);
return sqlSessionFactory;
}
@Bean
public DataSourceTransactionManager videoDataSourceTransactionManager(@Qualifier("videoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate VideoSqlSessionTemplate(@Qualifier("videoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
-
MybatisInterceptor是拦截器处理分页的
-
MybatisSqlCustomLog是继承Slf4jImpl简单重写的日记处理
3.基类包扫描
@Configuration
public class MapperScanConfig {
@Bean(name = "entryMapperScannerConfigurer")
public MapperScannerConfigurer entryMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setMarkerInterface(EntryMapper.class);
mapperScannerConfigurer.setSqlSessionFactoryBeanName("entrySqlSessionFactory");
mapperScannerConfigurer.setbasePackage("com.ca.encyclopedia.api.dao");
return mapperScannerConfigurer;
}
@Bean(name = "videoMapperScannerConfigurer")
public MapperScannerConfigurer videoMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setMarkerInterface(VideoMapper.class);
mapperScannerConfigurer.setSqlSessionFactoryBeanName("videoSqlSessionFactory");
mapperScannerConfigurer.setbasePackage("com.ca.encyclopedia.api.dao");
return mapperScannerConfigurer;
}
}
@Configuration
public class EntryDataSourceConfig {
@Autowired
private MybatisInterceptor mybatisInterceptor;
@ConfigurationProperties(prefix = "spring.datasource.entry")
@Bean(name = "entryDataSource")
@Primary
public DataSource entryDataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entrySqlSessionFactory")
public SqlSessionFactory entrySqlSessionFactory(@Qualifier("entryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//设置分页拦截器
sqlSessionFactoryBean.setPlugins(new Interceptor[]{mybatisInterceptor});
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
//驼峰配置
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
sqlSessionFactory.getConfiguration().setLogImpl(MybatisSqlCustomLog.class);
return sqlSessionFactory;
}
@Primary
@Bean
public DataSourceTransactionManager entryDataSourceTransactionManager(@Qualifier("entryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean
public SqlSessionTemplate entrySqlSessionTemplate(@Qualifier("entrySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Configuration
public class VideoDataSourceConfig {
@Autowired
private MybatisInterceptor mybatisInterceptor;
@ConfigurationProperties(prefix = "spring.datasource.video")
@Bean(name = "videoDataSource")
public DataSource videoDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "videoSqlSessionFactory")
public SqlSessionFactory videoSqlSessionFactory(@Qualifier("videoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//设置分页拦截器
sqlSessionFactoryBean.setPlugins(new Interceptor[]{mybatisInterceptor});
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
//驼峰配置
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
sqlSessionFactory.getConfiguration().setLogImpl(MybatisSqlCustomLog.class);
return sqlSessionFactory;
}
@Bean
public DataSourceTransactionManager videoDataSourceTransactionManager(@Qualifier("videoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate VideoSqlSessionTemplate(@Qualifier("videoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
-
MybatisInterceptor是拦截器处理分页的
-
MybatisSqlCustomLog是继承Slf4jImpl简单重写的日记处理
3.基类包扫描
@Configuration
public class MapperScanConfig {
@Bean(name = "entryMapperScannerConfigurer")
public MapperScannerConfigurer entryMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setMarkerInterface(EntryMapper.class);
mapperScannerConfigurer.setSqlSessionFactoryBeanName("entrySqlSessionFactory");
mapperScannerConfigurer.setbasePackage("com.ca.encyclopedia.api.dao");
return mapperScannerConfigurer;
}
@Bean(name = "videoMapperScannerConfigurer")
public MapperScannerConfigurer videoMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setMarkerInterface(VideoMapper.class);
mapperScannerConfigurer.setSqlSessionFactoryBeanName("videoSqlSessionFactory");
mapperScannerConfigurer.setbasePackage("com.ca.encyclopedia.api.dao");
return mapperScannerConfigurer;
}
}
注:
- dao继承基类时,不能使用@Repository等注入注解
- boot启动类上也不能@MapperScan扫描到使用上面数据源的dao路径
使用示例:
public interface WikiEntryInfoDao extends EntryMapper {
@Select({"select * from wiki_entry_info where audit_status = 1 and id = #{id}"})
WikiEntryInfo selectById(@Param("id") Integer id);
}
public interface WikiVideoInfoDao extends VideoMapper {
@Select("select * from wiki_video_info where category_id = #{categoryId} and audit_status = 1 order by id desc")
List selectByCategoryId(@Param("categoryId") Integer categoryId);
}



