1.编写 application.properties文件
2.编写 DataSourceconfig
@Configuration
public class DataSourceConfig {
@Bean(name = "xycMasterDS")
@ConfigurationProperties(prefix = "spring.datasource.xyc-master") // application.properteis中对应属性的前缀
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "xycDs2")
@ConfigurationProperties(prefix = "spring.datasource.xyc-second") // application.properteis中对应属性的前缀
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
/**
@Bean(name = "dynamicDS1")
public DataSource dataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSource1());
// 配置多数据源
Map<Object, Object> dsMap = new HashMap(5);
dsMap.put("xyc-master", dataSource1());
dsMap.put("xyc-second", dataSource2());
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
}
**/
}
3.配置 MybatisDbAConfig A数据源
@Configuration
@MapperScan(basePackages = “com.xyc.hello4.*.mapper”, sqlSessionFactoryRef = “sqlSessionFactory1”)
public class MybatisDbAConfig {
@Autowired
@Qualifier("xycMasterDS")
private DataSource ds1;
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1); // 使用titan数据源, 连接titan库
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:com/xyc/hello4/*/mapper/xml/*Mapper.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
return template;
}
}
4.配置 MybatisDbBConfig
@Configuration
@MapperScan(basePackages = “com.xyc.hello5.getSUser.mapper”, sqlSessionFactoryRef = “sqlSessionFactory2”)
public class MybatisDbBConfig {
@Autowired
@Qualifier("xycDs2")
private DataSource ds2;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds2); // 使用titan数据源, 连接titan库
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:com/xyc/hello5/*/mapper/xml/*Mapper.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2()); // 使用上面配置的Factory
return template;
}
}
5.测试 。。
出现问题 1:invalid bound statement (not found) 解决:https://blog.csdn.net/myth_g/article/details/82154489
2:jdbcUrl is required with driverClassName 解决:https://my.oschina.net/chinesedragon/blog/1647846
6.动态设置 多数据源
(1)定义一个ContextHolder, 用于保存当前线程使用的数据源名
(2) .创建 DynamicDataSource
3.在 DataSourceConfig 配置文件里添加
4. 编写 注解
5.编写 注解AOP事物
@Aspect
@Component
public class DynamicDataSourceAspect {
@Before("@annotation(DS)")
public void beforeSwitchDS(JoinPoint point){
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DEFAULT_DS;
try {
// 得到访问的方法对象
Method method = className.getMethod(methodName, argClass);
// 判断是否存在@DS注解
if (method.isAnnotationPresent(DS.class)) {
DS annotation = method.getAnnotation(DS.class);
// 取出注解中的数据源名
dataSource = annotation.value();
}
} catch (Exception e) {
e.printStackTrace();
}
DataSourceContextHolder.setDB(dataSource);
}
@After("@annotation(DS)")
public void afterSwitchDS(JoinPoint point){
DataSourceContextHolder.clearDB();
}
}
6.更改 MyBatisDBConfig 把sqlSessionFactory里用的DataSource 设置为 动态数据源
7.使用 在
*ServiceImpl里