About the configuration of multiple data sources, I have introduced the configuration of multiple data sources of JdbcTemplate with you. That is relatively simple. This article will talk about the configuration of multiple data sources of MyBatis with you.
In fact, as for multi data sources, my attitude is the same as before. For complex data sources, I will directly go to distributed database middleware, and simply consider multi data sources. This is the suggestion in the project. In terms of technology, of course, all kinds of technologies should be mastered.
Engineering creation
First, you need to create the MyBatis project. The project creation is the same as the previous one. Add the MyBatis, MySQL, and Web dependencies:
After the project is created, add Druid dependency. Like JdbcTemplate, add Druid dependency here must be a druid specially built for Spring Boot. Traditional Druids cannot be used. The complete dependencies are as follows:
<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>
Multi data source configuration
Next, configure multiple data sources, which is basically the same as the configuration of multiple data sources of the JdbcTemplate. First, configure the basic database information in application.properties, and then provide two datasources. Here I will post the code. For the principle and framework in it, please refer to the previous article, which will not be described here.
Configuration in application.properties:
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 two more datasources are provided, 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 the configuration of MyBatis, which is different from the JdbcTemplate. MyBatis is a little bit more cumbersome to configure, because there are two beans to provide, so I will configure the two data sources separately in two classes. First, I will look at the configuration of the first data source:
@Configuration @MapperScan(basePackages = "org.javaboy.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 the mybatisconfiguone class. First, indicate that the class is a configuration class. The package to be scanned in the configuration class is org.javaboy.mybatis.mapper1, that is, the Mapper interface under the package will operate the data in dsOne. The corresponding SqlSessionFactory and SqlSessionTemplate are sqlSessionFactory1 and sqlSessionTemplate1 respectively. In mybatisconfiguone, provide sqlsession respectively Nfactory and SqlSessionTemplate are enough. SqlSessionFactory is created according to dsOne, and then a SqlSessionTemplate is created according to the created SqlSessionFactory.
After the configuration is completed, the second data source can be configured according to this configuration:
@Configuration @MapperScan(basePackages = "org.javaboy.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()); } }
OK, so the MyBatis multi data source is basically configured. Next, you only need to provide different mappers in the org.javaboy.mybatis.mapper1 and org.javaboy.mybatis.mapper2 packages, and inject different mappers into the Service to operate different data sources.
mapper creation
mapper in org.javaboy.mybatis.maper1:
public interface UserMapperOne { List<User> getAllUser(); }
Corresponding XML file:
<?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.javaboy.mybatis.mapper1.UserMapperOne"> <select id="getAllUser" resultType="org.javaboy.mybatis.model.User"> select * from t_user; </select> </mapper>
Mapper in org.javaboy.mybatis.mapper2:
public interface UserMapper { List<User> getAllUser(); }
Corresponding XML file:
<?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.javaboy.mybatis.mapper2.UserMapper"> <select id="getAllUser" resultType="org.javaboy.mybatis.model.User"> select * from t_user; </select> </mapper>
Next, inject two different mappers into the Service, and different mappers will operate different data sources.
Well, that's all for MyBatis multi data source article.