其他分享
首页 > 其他分享> > springmvc增加多个数据源

springmvc增加多个数据源

作者:互联网

在原来数据源上再加两个数据源,一个audit,一个sso

配置的application.properties

proxool.maximum.connection.count=40
proxool.minimum.connection.count=10
proxool.statistics=1m,15m,1h,1d
proxool.simultaneous.build.throttle=30
proxool.trace=false
connection.driverClassName=com.mysql.jdbc.Driver

connection.nei.audit.url=jdbc:mysql://ip2:3306/audit4?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
connection.nei.audit.username=root
connection.nei.audit.password=password

connection.nei.sso.url=jdbc:mysql://ip2:3306/sso?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
connection.nei.sso.username=root
connection.nei.sso.password=password

配置的applicationContext.xml

	
	<!-- nei.audit -->
	<bean id="dataSource2" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
		<!-- 数据源别名 -->
		<property name="alias" value="proxoolDataSource2"/>
		<property name="driver" value="${connection.driverClassName}" />
		<property name="driverUrl" value="${connection.nei.audit.url}" />
		<property name="user" value="${connection.nei.audit.username}" />
		<property name="password" value="${connection.nei.audit.password}" />
		<!--最大连接数(默认5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
		<property name="maximumConnectionCount" value="${proxool.maximum.connection.count}"/>
		<!--最小连接数(默认2个)-->  
		<property name="minimumConnectionCount" value="${proxool.minimum.connection.count}" />
		<property name="statistics" value="${proxool.statistics}" />
		<property name="simultaneousBuildThrottle" value="${proxool.simultaneous.build.throttle}"/>
</bean>
	
	<!-- nei.sso -->
	<bean id="dataSource3" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
		<!-- 数据源别名 -->
		<property name="alias" value="proxoolDataSource3"/>
		<property name="driver" value="${connection.driverClassName}" />
		<property name="driverUrl" value="${connection.nei.sso.url}" />
		<property name="user" value="${connection.nei.sso.username}" />
		<property name="password" value="${connection.nei.sso.password}" />
		<!--最大连接数(默认5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
		<property name="maximumConnectionCount" value="${proxool.maximum.connection.count}"/>
		<!--最小连接数(默认2个)-->  
		<property name="minimumConnectionCount" value="${proxool.minimum.connection.count}" />
		<property name="statistics" value="${proxool.statistics}" />
		<property name="simultaneousBuildThrottle" value="${proxool.simultaneous.build.throttle}"/>
	</bean>
	<!-- audit jdbc模板  + 命名参数模板 -->
	<bean id="auditJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource2" />
	</bean>
	<bean id="auditNamedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
		<constructor-arg ref="dataSource2" />
	</bean>
	
	<!-- sso jdbc模板  + 命名参数模板 -->
	<bean id="ssoJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource3" />
	</bean>
	<bean id="ssoNamedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
		<constructor-arg ref="dataSource3" />
	</bean>

AuditImpl

	@Autowired
	@Qualifier("auditJdbcTemplate")
    private JdbcTemplate jdbcTemplate;
	
	@Autowired
	@Qualifier("auditNamedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

SsoImpl

	@Autowired
	@Qualifier("ssoJdbcTemplate")
    private JdbcTemplate ssoJdbctemplate;
	
	@Autowired
	@Qualifier("ssoNamedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate ssonamedParameterJdbcTemplate;

标签:多个,nei,springmvc,数据源,Autowired,sso,connection,proxool
来源: https://blog.csdn.net/weixin_43075704/article/details/100016290