There are two kinds of requirements for multi-database configuration: one is that the project is too big and the amount of visits is too high, so many applications have to distribute multiple databases to ease the pressure of accessing. The other is that two different database services should be integrated together now, even if the database is different, one is mysql. A SQL server, Xiaobian's current project belongs to the latter.
To achieve the separation of reading and writing, first of all, we must ensure that master-slave replication, that is, the data of the write database can be replicated to the read database in real time, so as to ensure that there is no difference between the data. This is not what we want to learn today. The small edition project is not used at present. This article is about multi-database configuration. There are two ways to integrate multiple databases: subcontracting and AOP. This paper only records AOP.
Refer to this article: https://www.cnblogs.com/weixupeng/p/9720472.html (typesetting is not very friendly)
1. Because of the use of Maven, first pom.xml introduces the driver jar package dependency to link the database, mysql can be introduced directly, but SQL server and orcale need to download to the local first, and then manually introduce before adding dependencies, such as in this SQL server example article: https://www.cnblogs.com/dawnheaven/p/5738477.html You can also download the latest version from other sources, but remember to write the correct version number when mvn.
mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0
Remember to change to your own version - Dfile= "absolute path of jar package + full file name version"
This approach is also applicable to Linux environments.
2. Configure the properties file. The name of this file may be different for everyone. Some people create new db.properties and some config.properties. This is irrelevant. Configure their own files in spring.xml file.
3. Configure spring.xml. This file also has many different names. Some articles use application-content.xml, some files are called spring mvc.xml, my name is spring-context.xml. This depends on which one I use for my project. Anyway, there are the following quotations
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
Some say the last line is http://Www.spring framework.org/schema/aop/spring-aop-3.0.xsd is sometimes said to be http://www.spring framework.org/schema/aop/spring-aop.xsd
Dependencies need to be added to pom.xml:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency>
Other configurations are almost the same according to other articles, only one point needs attention!
<bean id="dynamicDataSource" class="com.dataSourcer.ThreadLocalRountingDataSource"> <property name="defaultTargetDataSource" ref="dataSource_daka"/> <property name="targetDataSources"> <map key-type="com.dataSourcer.DataSources"> This place needs to use enumeration classes if the enumeration is defined by Java code, otherwise java.lang.String can be used. <entry key= "daka" value-ref= "dataSource_daka"> </entry> This should be consistent with the key value of the custom enumeration class. <entry key="kaoqin" value-ref="dataSource_kaoqin"></entry> </map> </property> </bean>
4. Define your own database enumeration class.
5. Defining the ThreadLocalRountingDataSource class inherits from AbstractRoutingDataSource, which is basically the same as nothing special.
6. Customize annotation classes, which is very important but has nothing to say.
7. Define the database management class DataSourceType Manager (some named after DataSourceContextHolder, which is better understood by the Manager personally). ThreadLocal in this class still needs to be added to achieve thread safety, and the following is especially recommended:
// ThreadLocal class is the key to thread security because most data operations are executed concurrently, so thread security must be considered. private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>() { @Override protected DataSources initialValue() { return DataSources.daka; } };
8. The final highlight is DynamicDataSourceAspect, which defines the cut-point, and of course spring.xml.
Finally, the following is a summary of the code for my project:
config.propertiesjdbc.url = jdbc:mysql://IP:3306 / database name? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false jdbc.username = Account number jdbc.password= Password sql.url = jdbc:sqlserver://ip:1433;databaseName = database name sql.username = Account number sql.password= Password