1. 程式人生 > >springboot配置多資料來源,註解操作資料庫

springboot配置多資料來源,註解操作資料庫

最近新搭建了一個專案,需要去不同的資料庫中查詢資料,需要多個數據源,在網上搜索了下,基本上實現都很複雜,下面我自己實現了一個很簡單的配置方法。

1、原來我們都是在application.yml檔案中配置資料來源,現在不需要在application.yml檔案中配置了。

      新建jdbc.properties檔案,內容如下:

2、新建beans.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
		http://www.springframework.org/schema/util
		http://www.springframework.org/schema/util/spring-util-4.1.xsd 
		http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
	
	<mvc:annotation-driven />
 
	<!-- 掃描包裡下的所有class檔案,配置註解的類全都裝入容器中進行管理 -->
	<context:component-scan base-package="com.ceshi.tuc" />

	<!-- 資料庫配置資訊引入 -->
	<util:properties id="dbConfig" location="classpath:config/jdbc.properties" />

	<!-- datasource -->
	
	<bean primary="true" id="aiGuPiaoDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="url" value="#{dbConfig['trends.url']}" />
		<property name="username" value="#{dbConfig['trends.user']}" />
		<property name="password" value="#{dbConfig['trends.password']}" />
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="10000" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>

	<bean id="ymdDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="url" value="#{dbConfig['ymd.url']}" />
		<property name="username" value="#{dbConfig['ymd.user']}" />
		<property name="password" value="#{dbConfig['ymd.password']}" />
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="10000" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>
	
	
	<bean id="tucDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="url" value="#{dbConfig['tuc.url']}" />
		<property name="username" value="#{dbConfig['tuc.user']}" />
		<property name="password" value="#{dbConfig['tuc.password']}" />
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="10000" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>
	
	<!-- sessionFactory -->
	<bean primary="true" id="aiGuPiaoSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="aiGuPiaoDataSource" />
	</bean>
	
	<bean id="ymdSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="ymdDataSource" />
	</bean>
	
	<bean id="tucSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="tucDataSource" />
	</bean>
	
	<!-- mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ceshi.tuc.mapper.trends" />
		<property name="annotationClass"
			value="com.zhigunet.tyq.fbase.MapperAnnotation" />
		<property name="sqlSessionFactoryBeanName" value="aiGuPiaoSessionFactory"/>
	</bean>
	
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ceshi.tuc.mapper.ymd" />
		<property name="annotationClass"
			value="com.zhigunet.tyq.fbase.MapperAnnotation" />
		<property name="sqlSessionFactoryBeanName" value="ymdSessionFactory"/>
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ceshi.tuc.mapper.stock" />
		<property name="annotationClass"
			value="com.zhigunet.tyq.fbase.MapperAnnotation" />
		<property name="sqlSessionFactoryBeanName" value="tucSessionFactory"/>
	</bean>
	
</beans>

3、新建MapperAnnotation註解

4、新建FBaseConfig註解

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling;

@Target({java.lang.annotation.ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ImportResource({"classpath:config/tuc-beans.xml"})
@EnableScheduling	
@SpringBootApplication(exclude={DataSourceTransactionManagerAutoConfiguration.class, DataSourceAutoConfiguration.class})
public @interface FBaseConfig {

}

5、修改Application.java

@FBaseConfig
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx =  SpringApplication.run(Application.class, args);
    }
}

 

 

啟動專案直接執行,就可以實現了