和yml配置在SpringBoot+Mybatis+Durid整合多数据源的三种方式,第一种
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.4.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.19</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
闲话少说,上代码!!
我的项目结构:
第一步:配置类
- DataSourceType
package com.qiaokeji.aopdatasource.datasourceconfig;
/**
* 任务:
* 描述:
* 作者:YJG86166
* 日期:2020/5/20 10:09
* @author 86166
*/
public class DataSourceType {
/**数据源*/
public enum DataBaseType {
ARRAIGN,DZBL
}
/** 使用ThreadLocal保证线程安全*/
private static final ThreadLocal<DataBaseType> TYPE = new ThreadLocal<DataBaseType>();
/** 往当前线程里设置数据源类型*/
public static void setDataBaseType(DataBaseType dataBaseType) {
if (dataBaseType == null) {
throw new NullPointerException();
}
System.err.println("[将当前数据源改为]:" + dataBaseType);
TYPE.set(dataBaseType);
}
/** 获取数据源类型*/
public static DataBaseType getDataBaseType() {
DataBaseType dataBaseType = TYPE.get() == null ? DataBaseType.ARRAIGN : TYPE.get();
System.err.println("[获取当前数据源的类型为]:" + dataBaseType);
return dataBaseType;
}
/** 清空数据类型*/
public static void clearDataBaseType() {
TYPE.remove();
}
}
- DynamicDataSource
package com.qiaokeji.aopdatasource.datasourceconfig;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 任务:
* 描述:
* 作者:YJG86166
* 日期:2020/5/20 10:08
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
DataSourceType.DataBaseType dataBaseType = DataSourceType.getDataBaseType();
return dataBaseType;
}
}
- DataSourceConfig
package com.qiaokeji.aopdatasource.datasourceconfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* 任务:
* 描述:
* 作者:YJG86166
* 日期:2020/5/20 10:20
* @author 86166
*/
@Configuration
@MapperScan(basePackages = "com.alimather.myitem.mapper", sqlSessionFactoryRef = "SqlSessionFactory")
public class DataSourceConfig {
@Bean(name = "arraignDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.arraign")
public DataSource getDateSourceArraign() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dzblDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dzbl")
public DataSource getDateSourceDzbl() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
public DynamicDataSource DataSource(@Qualifier("arraignDataSource") DataSource arraignDataSource,
@Qualifier("dzblDataSource") DataSource dzblDataSource) {
Map<Object, Object> targetDataSource = new HashMap<>(2);
targetDataSource.put(DataSourceType.DataBaseType.ARRAIGN, arraignDataSource);
targetDataSource.put(DataSourceType.DataBaseType.DZBL, dzblDataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSource);
dataSource.setDefaultTargetDataSource(arraignDataSource);
return dataSource;
}
@Bean(name = "SqlSessionFactory")
public SqlSessionFactory arraignSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
}
- DataSourceAop
package com.qiaokeji.aopdatasource.datasourceconfig;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* 任务:
* 描述:
* 作者:YJG86166
* 日期:2020/5/20 10:25
* @author 86166
*/
@Aspect
@Component
public class DataSourceAop {
@Before("execution(* com.qiaokeji.aopdatasource.service..*.*Arraign*(..))")
public void setDataSourceArraign() {
System.err.println("ARRAIGN数据源");
DataSourceType.setDataBaseType(DataSourceType.DataBaseType.ARRAIGN);
}
@Before("execution(* com.qiaokeji.aopdatasource.service..*.*Dzbl*(..))")
public void setDataSourceDzbl() {
System.err.println("DZBL数据源");
DataSourceType.setDataBaseType(DataSourceType.DataBaseType.DZBL);
}
}
第二步:开始使用
原理就是在配置类里面配置的方法名,通过AOP读取方法特定的字符来切换数据源的使用
- service层
package com.qiaokeji.aopdatasource.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiaokeji.aopdatasource.bean.SysAclModule;
import com.qiaokeji.aopdatasource.mapper.SysAclModuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
/**
* 任务:
* 描述:
* 作者:YJG86166
* 日期:2020/5/20 9:05
* @author 86166
*/
@Service
public class SysAclModuleService {
@Autowired
private SysAclModuleMapper sysAclModuleMapper;
public HashMap<String,Object> listSysAclModuleDzbl(int page,int size){
HashMap<String,Object> map = new HashMap<>(2);
PageHelper.startPage(page,size);
Page<SysAclModule> sysAclModulePage = sysAclModuleMapper.listSysAclModule();
List<SysAclModule> sysAclModules = sysAclModulePage.getResult();
long total = sysAclModulePage.getTotal();
map.put("data",sysAclModules);
map.put("total",total);
return map;
}
}
- mapper接口
package com.qiaokeji.aopdatasource.mapper;
import com.github.pagehelper.Page;
import com.qiaokeji.aopdatasource.bean.SysAcl;
import org.apache.ibatis.annotations.Mapper;
/**
* @author 86166
*/
@Mapper
public interface SysAclMapper {
/**获取表信息
* @return 分页表信息*/
Page<SysAcl> listSysAcl();
}
其余两种方式,请进入我的博客主页查看,推荐第三种方式,很灵活。