Scenario assumes that: the existing electricity supplier business, commodities and stocks were placed in different libraries
Configuring a database connection
app:
datasource:
first:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1/product?useSSL=false
username: root
password: root
configuration:
maximum-pool-size: 10
second:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1/stock?useSSL=false
username: root
password: root
configuration:
maximum-pool-size: 10
Add configuration class
FirstConfig
@Configuration
@MapperScan(
basePackages = {"com.karonda.springboot2datasourcesmybatis.dao.first"},// 1. dao 层所在的包
sqlSessionTemplateRef = "firstSqlSessionTemplate")
public class FirstConfig {
@Bean
@Primary
public SqlSessionTemplate firstSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(firstSqlSessionFactory());
}
@Bean
@Primary
public DataSourceTransactionManager firstTransactionManager(){
return new DataSourceTransactionManager(firstDataSource());
}
@Bean
@Primary
public SqlSessionFactory firstSqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(firstDataSource());
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/first/*.xml")); // 2. xml 所在路径
return factoryBean.getObject();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.first")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.first.configuration")
public DataSource firstDataSource() {
return firstDataSourceProperties()
.initializeDataSourceBuilder()
.type(HikariDataSource.class) // 3. 可以显示指定连接池,也可以不显示指定;即此行代码可以注释掉
.build();
}
}
SecondConfig
@Configuration
@MapperScan(
basePackages = {"com.karonda.springboot2datasourcesmybatis.dao.second"},
sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class SecondConfig {
@Bean
public SqlSessionTemplate secondSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(secondSqlSessionFactory());
}
@Bean
public DataSourceTransactionManager secondTransactionManager(){
return new DataSourceTransactionManager(secondDataSource());
}
@Bean
public SqlSessionFactory secondSqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(secondDataSource());
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/second/*.xml"));
return factoryBean.getObject();
}
@Bean
@ConfigurationProperties("app.datasource.second")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("app.datasource.second.configuration")
public DataSource secondDataSource() {
return secondDataSourceProperties()
.initializeDataSourceBuilder()
.type(HikariDataSource.class)
.build();
}
}
dao
public interface ProductMapper {
Product getOneById(int id);
}
public interface StockMapper {
Stock getOneByProductId(int productId);
}
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="com.karonda.springboot2datasourcesmybatis.dao.first.ProductMapper">
<resultMap id="BaseResultMap" type="com.karonda.springboot2datasourcesmybatis.entity.Product">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<sql id="Base_Column_List">
id, name
</sql>
<select id="getOneById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from product
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
<mapper namespace="com.karonda.springboot2datasourcesmybatis.dao.second.StockMapper">
<resultMap id="BaseResultMap" type="com.karonda.springboot2datasourcesmybatis.entity.Stock">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="product_id" jdbcType="INTEGER" property="productId" />
<result column="stock_count" jdbcType="INTEGER" property="stockCount" />
</resultMap>
<sql id="Base_Column_List">
id, product_id, stock_count
</sql>
<select id="getOneByProductId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from stock
where product_id = #{productId,jdbcType=INTEGER}
</select>
</mapper>
Examples of Use
@Component
public class Task {
@Autowired
private ProductMapper productMapper;
@Autowired
private StockMapper stockMapper;
@Scheduled(cron = "0/5 * * * * ? ")
public void job(){
final int productId = 1;
Product product = productMapper.getOneById(productId);
Stock stock = stockMapper.getOneByProductId(productId);
System.out.println("产品名称: " + product.getName() + ", 库存: " + stock.getStockCount());
}
}
Precautions
- Using multiple data sources, a configuration class @Primary to add annotations (and only need to add a class configuration)
- In the configuration class need to configure the path where the dao layer packet and where xml
to sum up
JPA configuration substantially identical to the use of multiple data sources, particularly an article can be compared on the Spring JPA articles Boot 2.x plurality of data source configuration
reference:
Spring Boot (seven): Mybatis Multiple Data Sources Minimalist solutions
Complete code: GitHub
I Java C # turn the newbie, please correct me if wrong or insufficient, thank you