文章目录
- 1. DB1配置
- 2. DB2配置
- 3. yml配置
- 4. 目标方法
- 5. 创建2个mapper扫描包
1. DB1配置
package com.mayikt.config;import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;/*** sys-admin数据源** @author gblfy* @date 2022-09-12*/
@Configuration
@MapperScan(basePackages = " com.mayikt.main.mapper", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DB1DataSourceConfig {static final String MAPPER_LOCATION = "classpath*:/mapping/db1/*.xml";@Primary@Bean("db1DataSource")@ConfigurationProperties(prefix = "spring.datasource.admin")public DataSource getDb1DataSource() {return DataSourceBuilder.create().build();}@Primary@Bean("db1SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {// SqlSessionFactoryBean bean = new SqlSessionFactoryBean();//整合mybatisplus时需要采用MybatisSqlSessionFactoryBeanMybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));return bean.getObject();}@Primary@Bean("db1SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}
2. DB2配置
package com.mayikt.config;import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;/*** sys-order数据源* https://blog.csdn.net/qq_41153943/article/details/124817002** @author gblfy* @date 2022-09-22*/
@Configuration
@MapperScan(basePackages = " com.mayikt.order.mapper", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DB2DataSourceConfig {static final String MAPPER_LOCATION = "classpath*:/mapping/db2/*.xml";@Bean("db2DataSource")@ConfigurationProperties(prefix = "spring.datasource.order")public DataSource getDb2DataSource() {return DataSourceBuilder.create().build();}@Bean("db2SqlSessionFactory")public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {// SqlSessionFactoryBean bean = new SqlSessionFactoryBean();//整合mybatisplus时需要采用MybatisSqlSessionFactoryBeanMybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));return bean.getObject();}@Bean("db2SqlSessionTemplate")public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}
3. yml配置
server:port: 8080
spring:main:allow-bean-definition-overriding: truedatasource:admin: # sys-admindriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sys-admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 123456order: # sys-orderdriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sys-order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 123456application:name: mayikt-order
4. 目标方法
@Autowiredprivate SysOrderMapper orderMapper;@Autowiredprivate MayiktDictionaryDataMapper dictionaryDataMapper;@Overridepublic String getTestDatasource() {// sys-order数据源 解决事务方案 jtaSysOrder sysOrder = new SysOrder("mayikt");int result = orderMapper.insert(sysOrder);// sys-admin数据源MayiktDictionaryData mayiktDictionaryData = dictionaryDataMapper.selectById(1);return mayiktDictionaryData.getName() + result;}
5. 创建2个mapper扫描包
创建2个mapper扫描包,main.mapper和order.mapper