springboot+mybatis+yml⽂件配置多数据源
记录⼀下java开发中多数据源的配置过程,
1.配置⽂件l
  personnel:#数据源1
driver-class-name: sql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3307/person?useSSL=false&useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&allowMultiQueries=true&allowPublicKey      username: root
password: *******
userauth:#数据源2
driver-class-name: sql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3307/user?useSSL=false&useUnicode=true&characterEncoding=utf8&us
eJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&allowMultiQueries=true&allowPublicKeyR      username: root
password: ******
2.不同数据源枚举:
public enum DataSourceEnum {master,slaver;}
3.枚举类⼯具 set
public class DataSourceContextHolder {
private static final ThreadLocal<DataSourceEnum> CONTEXT_HOLDER = new ThreadLocal<DataSourceEnum>() {
@Override
protected DataSourceEnum initialValue() {
return DataSourceEnum.master;
}
};
public static void setDataSourceType(DataSourceEnum type) {
CONTEXT_HOLDER.set(type);
}
public static DataSourceEnum getDataSourceType() {
return ();
}
public static void resetDataSourceType() {
CONTEXT_HOLDER.set(DataSourceEnum.master);
}
}
4.⾃定义注解
@Retention(RetentionPolicy.RUNTIME) // 在运⾏时可见
@Target(ElementType.METHOD) // 注解可以⽤在⽅法上
public @interface DataSourceTypeAnno { //使⽤⽅式在service层⽅法上添加@DataSourceTypeAnno(DataSourceEnum.数据源枚举类型)⽤于指定所使⽤的数据源
DataSourceEnum value() default DataSourceEnum.master; }
5.aop拦截:这⾥使⽤asp的
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.flect.MethodSignature;
import annotation.Order;
import org.springframework.stereotype.Component;
import flect.Method;
@Component
@Aspect
@Order(-100)//为了保证可以拦截到
public class DataSourceAspect {
@Pointcut("execution(* *.*..*(..)) " +    //这⾥扫描的切点包是主要是service层,根据service层⽅法的上边所说的的⾃定义注解,去判断所使⽤的数据源类型,并动态切换数据源
"&& @nfig.DataSourceTypeAnno)")
public void dataSourcePointcut() {
}
@Around("dataSourcePointcut()")
public Object doAround(ProceedingJoinPoint pjp) {
MethodSignature methodSignature = (MethodSignature) Signature();
Method method = Method();
DataSourceTypeAnno typeAnno = Annotation(DataSourceTypeAnno.class);
DataSourceEnum sourceEnum = typeAnno.value();
if (sourceEnum == DataSourceEnum.master) {
DataSourceContextHolder.setDataSourceType(DataSourceEnum.master);
} else if (sourceEnum == DataSourceEnum.slaver) {
DataSourceContextHolder.setDataSourceType(DataSourceEnum.slaver);
}
Object result = null;
try {
result = pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
}
return result;
}
}
6.继承AbstractRoutingDataSource 类,实现对应数据源key的切换
nfig;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
DataSourceType();
}
}
7.配置mybatisConfig=》数据源信息
import org.apache.ibatis.session.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;//org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;该包在springboot2.0被取代,⽬前⽹上的都是这个springboot1.5的包,所以会⼀直报错
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@MapperScan(basePackages = "*.mapper")//扫描dao层mapper接⼝
public class MyBatisConfig {
/**
* @return
* @throws Exception
* @Primary 必需指定⼀个且只能有⼀个主数据源,否则报错
*/
@Primary
@Bean("masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.userauth")//根据数据源前缀到l读取数据源信息//此处改变前缀可以改变默认数据源//
public DataSource masterDataSource() throws Exception {
ate().build();
}
spring怎么读取配置@Bean("slaverDataSource")
@ConfigurationProperties(prefix = "spring.datasource.personnel")//根据数据源前缀到l读取数据源信息//可以配置更多数据源,到前提是l中存在,⽽且也需要在枚举类中添加枚举类型
public DataSource slaverDataSource() throws Exception {
ate().build();
}
/**
* @Qualifier 根据名称进⾏注⼊,通常是在具有相同的多个类型的实例的⼀个注⼊(例如有多个DataSource类型的实例)
* @DataSourceTypeAnno(DataSourceEnum.master)事务⽅法需要指定数据源
*/
@Bean("dynamicDataSource")
public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slaverDataSource") DataSource slaverDataSource) {
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
targetDataSources.put(DataSourceEnum.master, masterDataSource);
targetDataSources.put(DataSourceEnum.slaver, slaverDataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);// 该⽅法是AbstractRoutingDataSource的⽅法
dataSource.setDefaultTargetDataSource(masterDataSource);// 默认的datasource设置为myTestDbDataSource
return dataSource;
}
/**
* 根据数据源创建SqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DynamicDataSource dynamicDataSource,
@Value("pe-aliases-package") String typeAliasesPackage) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dynamicDataSource);// 指定数据源(这个必须有,否则报错)
// 下边两句仅仅⽤于*.xml⽂件,如果整个持久层操作不需要使⽤到xml⽂件的话(只⽤注解就可以搞定),则不加
factoryBean.setTypeAliasesPackage(typeAliasesPackage);// 指定实体类所在的包        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/**/*l"));//扫描l⽂Object();
}
/**
* 配置事务管理器
*/
@Bean
public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {
return new DataSourceTransactionManager(dataSource);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。