SPRINGMVC Multi-database configuration __ Database

Source: Internet
Author: User

1. Create jdbc.properties files

#oracle
Jdbc.driver=oracle.jdbc.driver.oracledriver
Jdbc.url=jdbc:oracle:thin:@192.0.0.1:1521:orcl
Jdbc.username=ytj
Jdbc.password=ytj


#mysql
Jdbc.mysql.driver=com.mysql.jdbc.driver
Jdbc.mysql.url=jdbc:mysql://192.0.0.1:3306/med_gwc
Jdbc.mysql.username=med_gwc
Jdbc.mysql.password=med_gwc


Configure both the Oracle database and the MySQL database in the configuration file as follows:


<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xsi:schemalocation= "
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">

<!--ensure automatic injection of-->
<context:annotation-config/>

<!--Import Database configuration file-->
<context:property-placeholder location= "Classpath:jdbc.properties"/>
<!--Oracle Metabase connection pool-->
<bean id= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" >
<!--basic properties URL, user, password-->
<property name= "url" value= "${jdbc.url}"/>
<property name= "username" value= "${jdbc.username}"/>
<property name= "Password" value= "${jdbc.password}"/>

<!--configuration initialization size, minimum, maximum-->
<property name= "InitialSize" value= "1"/>
<property name= "Minidle" value= "1"/>
<property name= "maxactive" value= "/>"

<!--Configure the time to get the connection wait timeout-->
<property name= "maxwait" value= "60000"/>

<!--How long does the configuration interval take to detect and detect idle connections that need to be closed, in milliseconds-->
<property name= "Timebetweenevictionrunsmillis" value= "60000"/>

<!--Configure a connection to the minimum surviving time in the pool, in milliseconds-->
<property name= "Minevictableidletimemillis" value= "300000"/>

<!--<property name= "validationquery" value= "select ' 1 ' from Student"/>-->
<property name= "Testwhileidle" value= "true"/>
<property name= "Testonborrow" value= "false"/>
<property name= "Testonreturn" value= "false"/>

<!--open Pscache and specify the size of Pscache on each connection-->
<property name= "Poolpreparedstatements" value= "true"/>
<property name= "Maxpoolpreparedstatementperconnectionsize"
Value= "/>"
</bean>


<!--configuration of MyBatis sqlsession-->
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<property name= "mapperlocations" value= "Classpath:com/oracle/**/**/**/*mapper.xml"/>
</bean>

<!--<import resource= "Mapperconfigurr.xml"/>-->
<!--mapper interface for scanning MyBatis-->
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "Com.oracle.*.*.dao"/>
<property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory"/>
</bean>


<!--configuration transaction manager-->
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<!--<tx:method name= "get*" read-only= "true" rollback-for= "Java.lang.Exception"/>-->
<tx:method name= "query*" read-only= "true"/>
<tx:method name= "select*" read-only= "true"/>
<tx:method name= "insert*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception"/>
<!--<tx:method name= "*" rollback-for= "java.lang.Exception"/>--><!--default rollback mechanism is RuntimeException-->
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id= "service" expression= "Execution" (* com.oracle.sx.businessData.service. *serviceimpl.* (..)) " /><!--Declare all methods of the class that contain the service to use the transaction-->
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "service"/>
</aop:config>



<!--mysql configuration database connection pool-->
<bean id= "Mysqldatasource" class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= " Close ">
<!--basic properties URL, user, password-->
<property name= "url" value= "${jdbc.mysql.url}"/>
<property name= "username" value= "${jdbc.mysql.username}"/>
<property name= "Password" value= "${jdbc.mysql.password}"/>

<!--configuration initialization size, minimum, maximum-->
<property name= "InitialSize" value= "1"/>
<property name= "Minidle" value= "1"/>
<property name= "maxactive" value= "/>"

<!--Configure the time to get the connection wait timeout-->
<property name= "maxwait" value= "60000"/>

<!--How long does the configuration interval take to detect and detect idle connections that need to be closed, in milliseconds-->
<property name= "Timebetweenevictionrunsmillis" value= "60000"/>

<!--Configure a connection to the minimum surviving time in the pool, in milliseconds-->
<property name= "Minevictableidletimemillis" value= "300000"/>

<!--<property name= "validationquery" value= "select ' 1 ' from Student"/>-->
<property name= "Testwhileidle" value= "true"/>
<property name= "Testonborrow" value= "false"/>
<property name= "Testonreturn" value= "false"/>

<!--open Pscache and specify the size of Pscache on each connection-->
<property name= "Poolpreparedstatements" value= "true"/>
<property name= "maxpoolpreparedstatementperconnectionsize" value= "/>"
</bean>

<!--mysql configuration mybatis sqlsession-->
<bean id= "Mysqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "DataSource" ref= "Mysqldatasource"/>
<property name= "mapperlocations" value= "Classpath:com/mysql/**/**/*mapper.xml"/>
</bean>

<!--mysql Scan MyBatis mapper interface-->
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "Com.mysql.*.*.dao"/>
<property name= "Sqlsessionfactorybeanname" value= "Mysqlsessionfactory"/>
</bean>


<!--mysql configuration transaction manager-->
<bean id= "Mysqltransactionmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "Mysqldatasource"/>
</bean>


</beans>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.