SSM static multiple data sources

SSM static multiple data sources

Design idea: According to different data sources, the DAO layer is divided into different packages. Each DAO layer package corresponds to different data sources, but the functions are the same.
1. First add pom dependency

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>apache-tomcat-test</groupId>
  <artifactId>apache-tomcat-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>apache-tomcat-test Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>5.0.5.RELEASE</spring.version>
    <mybatis.version>3.4.0</mybatis.version>
    <mybatis-spring.version>1.3.0</mybatis-spring.version>
    <slf4j.version>1.7.12</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <druid.version>1.0.9</druid.version>
  </properties>

  <dependencies>

    <!-- 单元测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
      <scope>test</scope>
    </dependency>

    <!-- spring框架包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- spring框架包 -->
    <!-- mybatis框架包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis-spring.version}</version>
    </dependency>
    <!-- mybatis框架包 -->
    <!-- 数据库驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.5</version>
    </dependency>
    <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- log -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- 连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>${druid.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.6</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>apache-tomcat-test</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <port>8080</port>
            <path>/${build.finalName}</path>
            <uriEncoding>UTF-8</uriEncoding>
            <server>tomcat7</server>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.2</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>

        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <!--包含了src/main/java目录下的所有xml资源配置文件-->
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <!--包含了src/main/resources目录下的所有资源配置文件,有别的格式的配置文件时,不要写成**/*.xml-->
          <include>**/*</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>

  </build>
</project>

2. Write DAO layer code and modal entity class (omitted)

3. First, modify the package of the DAO layer.
Copy the code of the DAO layer. The two packages represent the operations of different data sources, but the functions of the two packages are the same.

Insert picture description here


Modify the DAO layer interface corresponding to the namespace in the xml file.

Insert picture description here


Insert picture description here


4. Write the service layer.
Here we use the 3307 library to add data and the 3309 library to read the data

public interface UserInfoService {

    List<UserInfo> findUserAll(UserInfoExample userInfoExample);
    int addUser(UserInfo userInfo);
}
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    MasterUserInfoMapper masterUserInfoMapper;
    @Resource
    SlaveUserInfoMapper slaveUserInfoMapper;

    public List<UserInfo> findUserAll(UserInfoExample userInfoExample) {
        return slaveUserInfoMapper.selectByExample(userInfoExample);
    }
    public int addUser(UserInfo userInfo) {
        return masterUserInfoMapper.insert(userInfo);
    }
}

5. Create spring data source file dataSource-Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--3307数据源配置-->
    <!-- 配置数据源 -->
    <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://47.110.157.82:3307/dome"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="masterSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="masterDataSource"/>
    </bean>
    <!-- Mapper动态代理开发,扫描dao接口包-->
    <bean id="masterMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="masterSqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.liu.masterMapper"/>
    </bean>

    <!--3309数据源配置-->
    <!-- 配置数据源 -->
    <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://47.110.157.82:3309/dome"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="slaveSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="slaveDataSource"/>
    </bean>
    <!-- Mapper动态代理开发,扫描dao接口包-->
    <bean id="slaveMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="slaveSqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.liu.slaveMapper"/>
    </bean>
</beans>

6. Map dataSource-Context.xml into the spring file

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

      <import resource="dataSource-Context.xml"></import>
       <!-- 扫描注解 -->
       <context:component-scan base-package="com.liu"/>
</beans>

7. Test
Since my two databases have achieved master-slave replication, here I use 3307 to add data and 3309 to fetch data to achieve read-write separation. If your two databases have nothing to do with each other, you will find that one of your data sources stores data, and the other data source takes data from the other database, which realizes the use of multiple data sources in the project

    public static void main(String[] args) {
        ApplicationContext applicationContext;
        UserInfoService userInfoService;
        applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        userInfoService=(UserInfoService)applicationContext.getBean("userInfoService");
        //通过数据源3307添加数据
        UserInfo userInfo= new UserInfo();
        userInfo.setName("赵七");
        userInfo.setAge(20);
        userInfoService.addUser(userInfo);
        //通过数据源3309读取数据
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andAgeIsNotNull();
        List<UserInfo> userAll = userInfoService.findUserAll(userInfoExample);
        System.out.println(userAll.get(0));
    }