SSM configuring multiple data sources to use annotations

Don't talk about it. Go straight to the code and configuration file

1. The name of the application-test-mysql.properties configuration file can be written with the type of properties (two data sources are MySQL and jdbc is orcale)

jdbc.driver.class=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/xzcomp
jdbc.username=root
jdbc.password=root
jdbco.driver.class=oracle.jdbc.driver.OracleDriver
jdbco.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbco.username=scott
jdbco.password=tiger
tomcat.username=admin
project.reporting.outputEncoding=UTF-8
project.build.sourceEncoding=UTF-8

#c3p0 configure
c3p0.minPoolSize=5
c3p0.maxPoolSize=30
c3p0.initialPoolSize=7
c3p0.maxIdleTime=0
c3p0.acquireIncrement=5
c3p0.breakAfterAcquireFailure=true
c3p0.testConnectionOnCheckin=true
c3p0.acquireRetryAttempts=60
c3p0.acquireRetryDelay=60000
c3p0.idleConnectionTestPeriod = 40
c3p0.automaticTestTable =C3P0TestTable

2. applicationContext.xml is the spring configuration file

Two data sources DataSource1 and DataSource1 use the method of automatic injection of objects here

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
	
	
	<!-- Database connection information is in properties In file -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:application-test-mysql.properties</value>
			</list>
		</property>
	</bean>
	<!-- To configure datasource conn  DB-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver.class}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<!--         <property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}"/> -->
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
        <property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>
        <property name="breakAfterAcquireFailure" value="${c3p0.breakAfterAcquireFailure}"/>
<!--         <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/> -->
 </bean>
 <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbco.driver.class}"/>
        <property name="jdbcUrl" value="${jdbco.url}"/>
        <property name="user" value="${jdbco.username}"/>
        <property name="password" value="${jdbco.password}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<!--         <property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}"/> -->
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
        <property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>
        <property name="breakAfterAcquireFailure" value="${c3p0.breakAfterAcquireFailure}"/>
<!--         <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/> -->
 </bean>
		<bean id="sqlSessionFactory" 
	class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- Load mybatis Profile for -->
		<!-- <property name="configLocation" 
		value="classpath:configuration.xml"></property> -->
		<!-- Load database information -->
		<property name="dataSource" ref="dataSource"></property>
		
		 <property name="mapperLocations"  value="classpath*:com/xz/model/*Mapper.xml" />  
              
        <property name="typeAliasesPackage" value="com.xz.model" />
	</bean>
	
	<bean id="sqlSessionFactory2" 
	class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- Load mybatis Profile for -->
		<!-- <property name="configLocation" 
		value="classpath:configuration.xml"></property> -->
		<!-- Load database information -->
		<property name="dataSource" ref="dataSource2"></property>
		
		 <property name="mapperLocations"  value="classpath*:com/xz/model/*Mapper.xml" />  
              
        <property name="typeAliasesPackage" value="com.xz.model" />
	</bean>
	
	<!-- sqlsession -->
	<bean id="st" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory">
		</constructor-arg>		
	</bean>
	<bean id="st2" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory2">
		</constructor-arg>	
	</bean>
	<!--  Packet level scanning -->
	<context:component-scan base-package="com">
	</context:component-scan>
	<mvc:annotation-driven />
	<mvc:default-servlet-handler/> 
	
	<!-- Enable transaction annotation drive -->  
    <tx:annotation-driven />
    
    <bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    
<!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
        p:prefix="/WEB-INF/view/" p:suffix=".jsp">  
        <property name="viewClass">             
            <value>org.springframework.web.servlet.view.JstlView</value>              
        </property>  
    </bean>   --> 
</beans>

3. Switching of data sources in the class. Note: SqlSessionTemplate does not use auto injection, but uses Resource to specify data sources to switch data sources

package com.xz.controller;

@Controller
@RequestMapping("/system/home/")
public class LoginAndRegisterController {
	@Autowired
	LogAndRegisterService ls;
	@Autowired 
	TestThread rt;
	@Resource(name="st2")
	SqlSessionTemplate st2;
	@Resource(name="st")
	SqlSessionTemplate st;

	@RequestMapping(value="/xcx",method=RequestMethod.POST)
	@ResponseBody
	public List<Map> threadControll(){
		List<Map> lm = st2.selectList("login");
		return lm;
	}
}

Anyway, I read a lot of simple things on the Internet, and talked about a lot of mess, personal test success

Tags: JDBC Spring xml Mybatis

Posted on Fri, 29 Nov 2019 21:04:48 -0800 by SpaceLincoln