Spring Boot integrate multiple data sources MyBatis

Project Creation

First you need to create MyBatis project, project creation and as the foregoing, add MyBatis, MySQL and Web dependent:

pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.28</version>
    <scope>runtime</scope>
</dependency>

Note: druid here using alibaba's druid-spring-boot-starter, rather than the traditional druid, druid tradition of no datasource instance, you need to configure our own, is not recommended. Because druid-spring-boot-starter provides DruidDataSourceBuilder dependent classes, this example can be used to construct a DataSource

application.properties configuration

spring.datasource.one.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.two.url=jdbc:mysql:///test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource

Then providing two DataSource, as follows:

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.one")
    DataSource dsOne() {
        return DruidDataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.two")
    DataSource dsTwo() {
        return DruidDataSourceBuilder.create().build();
    }
}

MyBatis configuration

Next is MyBatis configuration, unlike the JdbcTemplate, MyBatis configuration bit more difficult, because the two to provide Bean, so there will be two separate data sources I arranged in two classes, a first look at the first configuration data source

@Configuration
@MapperScan(basePackages = "org.javayihao.mybatis.mapper1",sqlSessionFactoryRef = "sqlSessionFactory1",sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MyBatisConfigOne {
    @Resource(name = "dsOne")
    DataSource dsOne;

    @Bean
    SqlSessionFactory sqlSessionFactory1() {
        SqlSessionFactory sessionFactory = null;
        try {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dsOne);
            sessionFactory = bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sessionFactory;
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplate1() {
        return new SqlSessionTemplate(sqlSessionFactory1());
    }
}

Create MyBatisConfigOne class, the first class is specified in a configuration, configuration, the package is to be scanned org.javayihao.mybatis.mapper1, i.e. Mapper interface in the packet data dsOne in operation, and the corresponding SqlSessionTemplate are SqlSessionFactory sqlSessionFactory1 and sqlSessionTemplate1, within MyBatisConfigOne, respectively, to provide SqlSessionFactory and SqlSessionTemplate, according to dsOne SqlSessionFactory created, and then create a SqlSessionTemplate according to create good SqlSessionFactory.

After this configuration, according to this configuration, the second data source again arranged to:

@Configuration
@MapperScan(basePackages = "org.javayihao.mybatis.mapper2",sqlSessionFactoryRef = "sqlSessionFactory2",sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MyBatisConfigTwo {
    @Resource(name = "dsTwo")
    DataSource dsTwo;

    @Bean
    SqlSessionFactory sqlSessionFactory2() {
        SqlSessionFactory sessionFactory = null;
        try {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dsTwo);
            sessionFactory = bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sessionFactory;
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplate2() {
        return new SqlSessionTemplate(sqlSessionFactory2());
    }
}

Such multiple data sources MyBatis basically configured, only needs to provide the following in different Mapper org.javayihao.mybatis.mapper1 org.javayihao.mybatis.mapper2 packages and different Mapper-Service can operate in different injection data source.
Create a mapper

org.javayihao.mybatis.mapper1 in the mapper:

public interface UserMapperOne {
    List<User> getAllUser();
}

Corresponding xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.javayihao.mybatis.mapper1.UserMapperOne">
    <select id="getAllUser" resultType="org.javayihao.mybatis.model.User">
        select * from t_user;
    </select>
</mapper>

org.javayihao.mybatis.mapper2 in the mapper:

public interface UserMapper {
    List<User> getAllUser();
}

Corresponding xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.javayihao.mybatis.mapper2.UserMapper">
    <select id="getAllUser" resultType="org.javayihao.mybatis.model.User">
        select * from t_user;
    </select>
</mapper>

We then injected in two different Mapper Service, different Mapper operation to different data sources

Published 260 original articles · won praise 112 · views 260 000 +

Guess you like

Origin blog.csdn.net/qq_34491508/article/details/103855002