SPRINGMVC Hibernate add multiple data sources
(Example of adding multiple data sources to class Sshe/sypro)
Note: Applies to projects with SPRINGMVC + Hibernate. Other frameworks can only be used for reference.
Configure Spring
Create a new spring data source configuration file, such as Spring-hibernate-aite.xml
How to create a new file without saying it.
The contents of the new spring data Source configuration file:
The content is able to copy the original Spring-hibernate.xml completely and then make the corresponding changes.
A.datasource the corresponding changes
<!-- Configure the Name property of the data source ①bean, it turns out that DataSource is now Datasourceaite --><bean name= "Datasourceaite" class= " Com.alibaba.druid.pool.DruidDataSource "init-method=" Init "destroy-method=" close "><property name=" url "value = "${jdbc_url_aite}"/><property name= "username" value= "${jdbc_username_aite}"/><property name= " Password "value=" ${jdbc_password_aite} "/><!--initialize connection size--><property name=" initialsize "value=" 0 "/> <!--connection pool maximum number of connections--><property name= "maxactive" value= "/><!--connection Pool Max spare--><property name=" Maxidle "value="/><!--connection Pool min spare--><property name= "Minidle" value= "0"/><!--get connection max wait time-->< Property Name= "Maxwait" value= "60000"/><property name= "Validationquery" value= "${validationquery}"/>< Property Name= "Testonborrow" value= "false"/><property name= "Testonreturn" value= "false"/><property name = "Testwhileidle" value= "true"/><!--configuration interval How often does a check detect the spare connection that needs to be closed, in milliseconds--><PROperty name= "Timebetweenevictionrunsmillis" value= "60000"/><!--Configure the minimum time for a connection to survive in the pool. Units are milliseconds--><property name= "Minevictableidletimemillis" value= "25200000"/><!--open removeabandoned Function-- <property name= "removeabandoned" value= "true"/><!--1800 seconds, i.e. 30 minutes--><property name= " Removeabandonedtimeout "value=" 1800 "/><!--off abanded connection output error log--><property name=" logabandoned "value=" True "/><!--monitoring database--><!--<property name=" Filters "value=" stat "/>--><property name=" Filters " Value= "Mergestat"/></bean>
b.sessionfactory the corresponding changes
<!--Configure Hibernate session factory. ①bean id attribute, the original ID is sessionfactory now changed to Sessionfactoryaite;②datasource ref, the original is datasource now changed to the top with a new Data source name Datasourceaite--><bean id= "Sessionfactoryaite" class= " Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name=" DataSource "ref=" Datasourceaite "/><property name=" hibernateproperties "><props><prop key=" Hibernate.hbm2ddl.auto ">none</prop><prop key=" Hibernate.dialect ">${hibernate.dialect}</prop><prop key=" Hibernate.show_sql ">${hibernate.show_sql}</prop><prop key=" Hibernate.format_sql ">${ hibernate.format_sql}</prop></props></property><!--to actively scan the Hibernate class file configured by annotation mode-->< Property Name= "Packagestoscan" ><list><value>sy.*.model</value></list></property ></bean>
c. Corresponding changes to the things manager
<!--<span style= "White-space:pre" ></span> Configure transaction manager <span style= "White-space:pre" ></span> ①bean's Name property, originally TransactionManager now changed to Transactionmanageraite<span style= "White-space:pre" ></span> ②sessionfactory ref, the original is sessionfactory now changed to the top with a new name Sessionfactoryaite<span style= "White-space:pre" ></ Span>--><span style= "White-space:pre" ></span><bean name= "Transactionmanageraite" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "><span style=" White-space:pre "></ Span><property name= "Sessionfactory" ref= "Sessionfactoryaite" ></property><span style= " White-space:pre "></span></bean>
D. Corresponding changes to the object interception rules
<!--interceptors to configure things ① with the newly created things manager above to manage the rules of things, the original ID is transactionadvice now changed to Transactionadviceaite② change ID. The original ID is transactionadvice now changed to Transactionadviceaite--><tx:advice id= "Transactionadviceaite" Transaction-manager= "Transactionmanageraite" ><tx:attributes><tx:method name= "add*" propagation= " REQUIRED "/><tx:method name=" append* "propagation=" REQUIRED "/><tx:method name=" save* "propagation=" REQUIRED "/><tx:method name=" update* "propagation=" REQUIRED "/><tx:method name=" modify* "propagation=" REQUIRED "/><tx:method name=" edit* "propagation=" REQUIRED "/><tx:method name=" del* "propagation=" REQUIRED "/><tx:method name=" delete* "propagation=" REQUIRED "/><tx:method name=" remove* "propagation=" REQUIRED "/><tx:method name=" Repair "propagation=" REQUIRED "/><tx:method name=" Delandrepair "propagation = "REQUIRED"/><tx:method name= "get*" propagation= "REQUIRED" read-only= "true"/><tx:method name= "find*" propagation= "REQUIRED" Read-only= "true"/><tx:method name= "load*" propagation= "REQUIRED" read-only= "true"/><tx:method name= " search* "propagation=" REQUIRED "read-only=" true "/><tx:method name=" datagrid* "propagation=" REQUIRED " Read-only= "true"/><tx:method name= "*" propagation= "REQUIRED" read-only= "true"/></tx:attributes> </tx:advice>
e.spring The corresponding changes to the facets configuration
<!--Add new rules to spring's Aop① change Advice-ref, Transactionadvice first changed to the top new name Transactionadviceaite--><aop:config ><aop:pointcut id= "transactionpointcut" expression= "Execution (* sy.*.service). *impl.* (..)) " /><aop:advisor pointcut-ref= "Transactionpointcut" advice-ref= "Transactionadviceaite"/></aop:config >
Save File
Here, the configuration of spring is basically finished.
change web. XML
To make the new data source configuration file Spring-hibernate-aite.xml Effective, you will need to add classpath in the Web. xml
<context-param><param-name>contextConfigLocation</param-name><!--Add Classpath: Spring-hibernate-aite.xml--><param-value>classpath:spring.xml,classpath:spring-hibernate.xml, Classpath:spring-hibernate-aite.xml</param-value></context-param>
Save File
This time. Be able to start the execution of the project to see if the error, in doing the next action
New DAO uses a new data source
There are several DAO that have several data sources
New Daoi
It is possible to copy the original Sy.dao.BaseDaoI.java directly into the same package folder and rename it to Sy.dao.AiteBaseDaoI.java
New Daoimpl
It is possible to copy the original Sy.dao.impl.BaseDaoImpl.java directly into the same package folder. Rename to Sy.dao.AiteBaseDaoImpl.java
Then make some minor adjustments:
/**repository originally was Basedao here changed into aitebasedao**/@Repository ("Aitebasedao") public class aitebasedaoimpl<t> Implements aitebasedaoi<t> {private static final Logger Logger=logger.getlogger (aitebasedaoimpl.class);/** Sessionfactory name is sessionfactory now changed into Sessionfactoryaite**/private sessionfactory sessionfactoryaite;public Sessionfactory Getsessionfactoryaite () {return sessionfactoryaite;} @Autowiredpublic void Setaitesessionfactory (Sessionfactory sessionfactoryaite) {this.sessionfactoryaite = Sessionfactoryaite;} Private Session getcurrentsession () {return this.sessionFactoryAite.getCurrentSession ();}
There's no change anywhere else.
After the DAO layer has been written. Ability to start the execution of the next project, see if there is an exception, and then proceed to the next step
Call of service layer
@Service ("Taskservice") public class Taskserviceimpl implements TASKSERVICEI {/** here with the new data source DAO is good. The others are consistent with the original **/private aitebasedaoi<ttask> tdao;public aitebasedaoi<ttask> Gettdao () {return tdao;} @Autowiredpublic void Settdao (aitebasedaoi<ttask> tdao) {This.tdao = Tdao;}
Here, from the configuration to the use of the finished, and then to complete your application it
SPRINGMVC Hibernate add multiple data sources (Sshe/sypro add multiple data sources as an example)