springboot配置多数据源,注解操作数据库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangyunsheng11/article/details/84978104

最近新搭建了一个项目,需要去不同的数据库中查询数据,需要多个数据源,在网上搜索了下,基本上实现都很复杂,下面我自己实现了一个很简单的配置方法。

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);
    }
}

启动项目直接运行,就可以实现了

猜你喜欢

转载自blog.csdn.net/zhangyunsheng11/article/details/84978104