Spring + Spring MVC + mybatis configuration dual data source and usage

Source: Internet
Author: User
Tags aop

I also consulted a number of resource information on the Internet, the summary is as follows:

First, JDBC configuration, I use the two databases are Oracle, users can according to their own situation to adjust the configuration information inside




Second, spring data source configuration

        <!--configuration Properties start--> <bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.confi" G.preferencesplaceholderconfigurer "> <!--<property name=" Systempropertiesmodename "value=" SYSTEM_ Properties_mode_override "/> <property name=" Ignoreresourcenotfound value= "false"/>--> <property N Ame= "Locations" > <list> <value>classpath:config/jdbc.properties</value> </list> ;/property> </bean> <!--configuration Properties End--> <!--Configure the first datasource data source start--> <bean id= "C3p0dat ASource1 "class=" Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "> <property name=" Driverclass "value=" ${jdbc.driver} "/> <property name=" Jdbcurl "value=" ${jdbc.url1} "/> <property name=" us Er "value=" ${jdbc.username1} "/> <property name=" password "value=" ${jdbc.password1} "/> <property name=" ac Quireincrement "value=" ${jdbc.acquireincrement} "/> <property name= "initialpoolsize" value= "${jdbc.initialpoolsize}"/> <property name= "minPoolSize" value = "${jdbc.minpoolsize}"/> <property name= "maxpoolsize" value= "${jdbc.maxpoolsize}"/> <property name= "max IdleTime "value=" ${jdbc.maxidletime} "/> <property name=" maxstatements "value=" ${jdbc.maxstatements} "/> ;p roperty name= "numhelperthreads" value= "${jdbc.numhelperthreads}"/> <property name= " Idleconnectiontestperiod "value=" ${jdbc.idleconnectiontestperiod} "/> </bean> <!--Configure the first datasource data source end --> <!--Configure the first DataSource data source to start--> <bean id= "C3p0datasource2" class= "Com.mchange.v2.c3p0.ComboPooledDataS" Ource "destroy-method=" Close "> <property name=" driverclass "value=" ${jdbc.driver} "/> <property name=" JD Bcurl "value=" ${jdbc.url2} "/> <property name=" user "value=" ${jdbc.username2} "/> <property name=" password "Value=" ${jdbc.password2} "/> <property name=" AcquIreincrement "value=" ${jdbc.acquireincrement} "/> <property name=" initialpoolsize "value=" ${ Jdbc.initialpoolsize} "/> <property name=" minpoolsize "value=" ${jdbc.minpoolsize} "/> <property name=" max Poolsize "value=" ${jdbc.maxpoolsize} "/> <property name=" maxidletime "value=" ${jdbc.maxidletime} "/> <pro Perty name= "maxstatements" value= "${jdbc.maxstatements}"/> <property name= "Numhelperthreads" value= Jdbc.numhelperthreads} "/> <property name=" idleconnectiontestperiod "value=" ${jdbc.idleconnectiontestperiod} "/> </bean> <!--Configure the first datasource data source end--> <!--Configure the data source switch tool class, which needs to be written by itself, followed by an introduction to--> <bean id="  
            Dynamicdatasource "class=" Com.odon.datasource.DynamicDataSource "> <property name=" targetdatasources "> <map key-type= "java.lang.String" > <entry value-ref= "C3p0datasource1" key= "C3p0data" Source1 "></entry> <entry value-ref="C3p0datasource2" key= "C3p0datasource2" ></entry> </map> </property> <property name= "Defaulttargetdatasource" ref= "C3p0datasource1" > <!--Configure the default data source--> T;/property> </bean> <!--create sqlsessionfactory start--> <bean id= "Sqlsessionfactory" O Rg.mybatis.spring.SqlSessionFactoryBean "> <!--referencing MyBatis profile--> <property name=" configlocation "value=" Classpath:config/mybatis-config.xml "/> <!--referencing data source configuration Here's what you need to be aware of. The tool class--> <property name=" DataSource "ref = "Dynamicdatasource"/> <!--reference Mapper profile--> <property name= "mapperlocations" value= "classpath:com/odon/" Mapper/*.xml "/> </bean> <!--create Sqlsessionfactory end--> <!--automatic Assembly DAO start--> <bean class=" org.m Ybatis.spring.mapper.MapperScannerConfigurer "> <property name=" basepackage "value=" Com.odon.dao " > </bean>;! --Automatically assemble DAO end--> <!--enable Spring annotation support--> <context:annotation-config/> <!--automatic scanning (automatic injection)--> <con Text:component-scan base-package= "Com.odon.service. * "/> <!--configuration transaction manager started--> <bean id=" Txmanager "class=" Org.springframework.jdbc.datasource.DataSourceTrans ActionManager "> <property name=" dataSource "ref=" C3p0datasource1 "></property> </bean> <!-- Configure the transaction manager to end the--> <!--basic transaction definition, using TransactionManager for transaction management, the default get*,find* method transaction is read-only--> <tx:advice id= "Txadv Ice "transaction-manager=" Txmanager "> <tx:attributes> <tx:method name=" find* "read-only=" true "/> & Lt;tx:method name= "get*" read-only= "true"/> <tx:method name= "update*" propagation= "REQUIRED"/> <tx:me Thod name= "delete*" propagation= "REQUIRED"/> <tx:method name= "save*" propagation= "REQUIRED"/> <tx:meth OD name= "*" propagation= "REQUIRED"/> </tx:attributes> </tx:advice>;! --The tangent of things defines AOP in ASPECTJ way the first * represents all return value types The second * represents all the classes and the third * represents all the methods of the class, the last one.
         Represents all the parameters. --> <aop:config> <aop:pointcut id= "interceptorpointcuts expression=" Execution (* com.odon.service.imp l.*impl.* (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "interceptorpointcuts"/> </aop:config>
Third, data source switch tool class

Package Com.odon.datasource;

Import Org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

 * @author Zhangqifeng

 * @date: 2017-11-1 pm 3:48:38
 * @version:
class Dynamicdatasource extends abstractroutingdatasource{

	protected Object Determinecurrentlookupkey ( ) {return
		 datasourcecontextholder.getdbtype ();
Four, data source switching method

Package Com.odon.datasource;

 * @author Zhangqifeng

 * @date: 2017-11-1 pm 3:49:23
 * @version:

class Datasourcecontextholder {

	private static final threadlocal<string> Contextholder = new threadlocal<string > ();

	Set the data source you want to use public  
	static void Setdbtype (String dbType) {
		contextholder.set (dbType);

	Gets the data source public  
	static String Getdbtype () {return
		contextholder.get ();  

	Clears the data source, using the default data source public  
	static void Cleardbtype () {
		contextholder.remove ();  

V. Data source Variable Class

Package Com.odon.datasource;

 * @author Zhangqifeng

 * @date: 2017-11-1 pm 3:51:52
 * @version:

class DatasourceType {
	//Data source 1 public  
	static final String source_1 = "C3p0datasource1";  
	Data source 2 public  
	static final String source_2 = "C3p0datasource2";  
Vi. Methods of Use

Package Com.odon.service.impl;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.stereotype.Service;
Import Com.odon.service.OAFileService;
Import Com.odon.dao.OAFileMapper;
Import Com.odon.datasource.DataSourceContextHolder;

Import Com.odon.datasource.DataSourceType; /** * @author Zhangqifeng * @date: 2017-11-1 pm 5:01:17 * @version: */@Service ("Oafileservice") public class Oafile

	Serviceimpl implements oafileservice{@Autowired private Oafilemapper oafilemapper; @Override public list<map<string,object>> selectoafilelist () {list<map<string,object>> List
		= new Arraylist<map<string,object>> ();
		map<string,object> _map = new hashmap<string,object> ();
			try {datasourcecontextholder.setdbtype (datasourcetype.source_2);
		List = Oafilemapper.selectoafilelist (_map); catch (ExceptIon e) {e.printstacktrace ();
	} return list; }


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.