如何在spring配置多个数据源

发布网友 发布时间:2022-04-23 14:36

我来回答

3个回答

懂视网 时间:2022-05-03 14:10

方法,此方法主要返回AbstractRoutingDataSource抽象类的多数据源的Key值

 

提醒:这里主要提醒一下,Spring Aop(AspectJ实现)的拦截方法,@Before@After注解之类的方法的参数可以具有一个 org.aspectj.lang.JoinPoint 类型的形参,而@Around类需要是ProceedingJoinPoint此形参,不要混用(官方说明来源:https://docs.spring.io/spring/docs/5.2.6.RELEASE/spring-framework-reference/core.html#aop-ataspectj-advice-params)

 

首先展示下配置文件:application.yml

spring:
 datasource:
 one:
 username: root
 password: root
 jdbc-url: jdbc:mariadb://localhost:3306/myschool
 driver-class-name: org.mariadb.jdbc.Driver
 two:
 username: root
 password: root
 jdbc-url: jdbc:mariadb://localhost:3306/myschool
 driver-class-name: org.mariadb.jdbc.Driver

配置多个数据源:

@Bean(value = "db1")
@ConfigurationProperties(prefix = "spring.datasource.one")
public HikariDataSource dataSource() {
HikariDataSource dataSource = new HikariDataSource();
return dataSource;
}
@Bean(value = "db2")
@ConfigurationProperties(prefix = "spring.datasource.two")
public HikariDataSource dataSource2() {
HikariDataSource dataSource = new HikariDataSource();
return dataSource;
}



下面是多数据源的配置:

技术图片

 

 

 

动态数据源实现如下:

技术图片

 

 

 

获取数据源的目标位置:DataSourceTransactionManager类的doBegin方法

技术图片

 

 

 

具体可以下载Demo试试:https://github.com/starSmallDream/MySpringDataJdbcAndMybatisByManyDataSource.git

 

SpringDataJdbc多数据源

标签:source   href   determine   sch   ref   style   github   key   lan   

热心网友 时间:2022-05-03 11:18

给你一个例子:
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

<jee:jndi-lookup id="mysqlds" jndi-name="java:comp/env/jdbc/mysqlds" />
<jee:jndi-lookup id="orads" jndi-name="java:comp/env/jdbc/orads" />

<bean id="dataSource" class="util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="0" value-ref="mysqlds" />
<entry key="1" value-ref="orads" />
</map>
</property>
<property name="defaultTargetDataSource" ref="orads" />
</bean>
</beans>

package util;

public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();

public static void setDbType(String dbType) {
contextHolder.set(dbType);
}

public static String getDbType() {
return (String) contextHolder.get();
}

public static void clearDbType() {
contextHolder.remove();
}
}

package util;

import java.sql.SQLException;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {
//static Logger log = Logger.getLogger("DynamicDataSource");
@Override
protected Object determineCurrentLookupKey() {
// TODO
return DbContextHolder.getDbType();
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}

}

测试程序:

DbContextHolder.setDbType("1");
UserDomain od = this.testService.queryUserAccount("admin");
System.out.println("username=="+od.getName());

DbContextHolder.setDbType("0");
d = this.testService.queryUserAccount("admin");

热心网友 时间:2022-05-03 12:36

配置两个SessionFactory

在DAO定义时指定sessionFactory

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com