springboot配置两个数据源怎么实现
2个回答
展开全部
a.直接配置2套数据源就像下面最EASY
@Configuration
public class SpringConfig {
@Bean
public DataSource dataSource(){
return new XXDataSource();
}
@Bean
public Counter dataSource2(){
return new XXDataSource();
}
}
b.复合数据源只读分离库实现 complexds
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:initialSize="${druid.initialSize}"
p:minIdle="${druid.minIdle}"
p:maxActive="${druid.maxActive}"
p:maxWait="${druid.maxWait}"
p:timeBetweenEvictionRunsMillis="${druid.timeBetweenEvictionRunsMillis}"
p:minEvictableIdleTimeMillis="${druid.minEvictableIdleTimeMillis}"
p:validationQuery="${druid.validationQuery}"
p:poolPreparedStatements="${druid.poolPreparedStatements}"
p:maxPoolPreparedStatementPerConnectionSize="${druid.maxPoolPreparedStatementPerConnectionSize}"/>
<bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
p:url="${slave.jdbc.url}"
p:username="${slave.jdbc.username}"
p:password="${slave.jdbc.password}"
p:initialSize="${slave.druid.initialSize}"
p:minIdle="${slave.druid.minIdle}"
p:maxActive="${slave.druid.maxActive}"
p:maxWait="${slave.druid.maxWait}"
p:timeBetweenEvictionRunsMillis="${slave.druid.timeBetweenEvictionRunsMillis}"
p:minEvictableIdleTimeMillis="${slave.druid.minEvictableIdleTimeMillis}"
p:validationQuery="${slave.druid.validationQuery}"
p:poolPreparedStatements="${slave.druid.poolPreparedStatements}"
p:maxPoolPreparedStatementPerConnectionSize="${slave.druid.maxPoolPreparedStatementPerConnectionSize}"/>
<bean id="complexDataSource" class="com.weghst.pine.complexds.ComplexDataSource">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1">
<map>
<entry key="#{T(com.weghst.pine.complexds.NamedDS).DEFAULT_SLAVE_NAME}" value-ref="slaveDataSource"/>
</map>
</constructor-arg>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="complexDataSource"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--
MyBatis 配置
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="complexDataSource"/>
<property name="configurationProperties">
<props>
<prop key="defaultScriptingLanguage">org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver</prop>
</props>
</property>
<property name="mapperLocations" value="classpath*:com/weghst/pine/repository/*Mapper.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="com.weghst.pine.util.FastSqlSessionTemplate"
c:sqlSessionFactory-ref="sqlSessionFactory"/>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.weghst.pine.repository"/>
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
</bean>
<bean id="complexDataSourceInterceptor" class="com.weghst.pine.complexds.ComplexDataSourceInterceptor"/>
<aop:config>
<aop:aspect ref="complexDataSourceInterceptor">
<aop:around method="invoke" pointcut="execution(* com.weghst.pine.repository.*.*(..))"/>
</aop:aspect>
</aop:config>
@Configuration
public class SpringConfig {
@Bean
public DataSource dataSource(){
return new XXDataSource();
}
@Bean
public Counter dataSource2(){
return new XXDataSource();
}
}
b.复合数据源只读分离库实现 complexds
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:initialSize="${druid.initialSize}"
p:minIdle="${druid.minIdle}"
p:maxActive="${druid.maxActive}"
p:maxWait="${druid.maxWait}"
p:timeBetweenEvictionRunsMillis="${druid.timeBetweenEvictionRunsMillis}"
p:minEvictableIdleTimeMillis="${druid.minEvictableIdleTimeMillis}"
p:validationQuery="${druid.validationQuery}"
p:poolPreparedStatements="${druid.poolPreparedStatements}"
p:maxPoolPreparedStatementPerConnectionSize="${druid.maxPoolPreparedStatementPerConnectionSize}"/>
<bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
p:url="${slave.jdbc.url}"
p:username="${slave.jdbc.username}"
p:password="${slave.jdbc.password}"
p:initialSize="${slave.druid.initialSize}"
p:minIdle="${slave.druid.minIdle}"
p:maxActive="${slave.druid.maxActive}"
p:maxWait="${slave.druid.maxWait}"
p:timeBetweenEvictionRunsMillis="${slave.druid.timeBetweenEvictionRunsMillis}"
p:minEvictableIdleTimeMillis="${slave.druid.minEvictableIdleTimeMillis}"
p:validationQuery="${slave.druid.validationQuery}"
p:poolPreparedStatements="${slave.druid.poolPreparedStatements}"
p:maxPoolPreparedStatementPerConnectionSize="${slave.druid.maxPoolPreparedStatementPerConnectionSize}"/>
<bean id="complexDataSource" class="com.weghst.pine.complexds.ComplexDataSource">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1">
<map>
<entry key="#{T(com.weghst.pine.complexds.NamedDS).DEFAULT_SLAVE_NAME}" value-ref="slaveDataSource"/>
</map>
</constructor-arg>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="complexDataSource"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--
MyBatis 配置
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="complexDataSource"/>
<property name="configurationProperties">
<props>
<prop key="defaultScriptingLanguage">org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver</prop>
</props>
</property>
<property name="mapperLocations" value="classpath*:com/weghst/pine/repository/*Mapper.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="com.weghst.pine.util.FastSqlSessionTemplate"
c:sqlSessionFactory-ref="sqlSessionFactory"/>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.weghst.pine.repository"/>
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
</bean>
<bean id="complexDataSourceInterceptor" class="com.weghst.pine.complexds.ComplexDataSourceInterceptor"/>
<aop:config>
<aop:aspect ref="complexDataSourceInterceptor">
<aop:around method="invoke" pointcut="execution(* com.weghst.pine.repository.*.*(..))"/>
</aop:aspect>
</aop:config>
展开全部
springboot实现多数据源有两种方案:
1、按照数据源分别把mapper和entity放到不同的package下,然后用两个数据源分别注册、扫描对应的package,独立的sessionfactoty
2、基于aop动态的切换的数据源
详情参照:https://www.94rg.com/article/8
希望能否解决你的问题,祝你生活愉快
1、按照数据源分别把mapper和entity放到不同的package下,然后用两个数据源分别注册、扫描对应的package,独立的sessionfactoty
2、基于aop动态的切换的数据源
详情参照:https://www.94rg.com/article/8
希望能否解决你的问题,祝你生活愉快
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询