导入依赖
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
编辑properties
#db1
spring.datasource.db1.url=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db1.username=xxx
spring.datasource.db1.password=xxx
#db2
spring.datasource.db2.url=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db2.username=xxx
spring.datasource.db2.password=xxx
编写db1config、db2congfig。两个数据库需要分为不同的文件
@Configuration
@MapperScan(basePackages = Db1DataConfig.PACKAGE, sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1DataConfig {
static final String PACKAGE = "com.xxx.xxx.db.dao.db1";
static final String MAPPER = "classpath:/mapper/db1/*.xml";
@Value("${spring.datasource.db1.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean(name = "db1DataSource")
@Primary
public DataSource dbDataSource() {
return DataSourceBuilder.create()
.driverClassName(driverClassName)
.username(username)
.password(password)
.url(url)
.build();
}
@Bean("db1SqlSessionFactory")
@Primary
public SqlSessionFactory dbSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
// 添加数据源
sessionFactoryBean.setDataSource(dataSource);
// 添加分页配置
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.addInterceptor(new PaginationInterceptor());
sessionFactoryBean.setConfiguration(configuration);
// 添加mapper目录
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER));
return sessionFactoryBean.getObject();
}
@Bean(name = "db1TransactionManager")
@Primary
public DataSourceTransactionManager dbTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
目录结构
遇到的问题
@Primary
主键注解只能存在一个,当有多个配置文件时需要注意
运行报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx
。问题原因:SalSessionFactory
不要使用原生的,请使用MybatisSalSessionFactory
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
需要添加分页插件,否则分页会失效
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.addInterceptor(new PaginationInterceptor());
sessionFactoryBean.setConfiguration(configuration);
运行报错:No qualifying bean of type 'org.springframework.transaction.TransactionManager
。问题原因:需增加指定事务的配置
/**
* 事务枚举方便引用
*
* @author wangmin
*/
public class TransactionConstant {
/**
* db1事务
*/
public static final String DB1_TRAN_MANAGER = "db1TranManager";
/**
* db2事务
*/
public static final String DB2_TRAN_MANAGER = "db2TranManager";
/**
* 链式事务
*/
public static final String CHAINED_TRAN_MANAGER = "chainedTranManager";
public TransactionConstant() {
}
}
@Configuration
public class TransactionManagerConfig {
/**
* db1事务
*
* @param dataSource 数据源
* @return PlatformTransactionManager
*/
@Primary
@Bean(TransactionConstant.DB1_TRAN_MANAGER)
public PlatformTransactionManager db1TranManage(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* db2事务
*
* @param dataSource 数据源
* @return PlatformTransactionManager
*/
@Bean(TransactionConstant.DB2_TRAN_MANAGER)
public PlatformTransactionManager db2TranManage(@Qualifier("db2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 链式事务
*
* @param db1DataSource 数据源
* @param db2DataSource 数据源
* @return PlatformTransactionManager
*/
@Bean(TransactionConstant.CHAINED_TRAN_MANAGER)
public PlatformTransactionManager chainedTranManage(@Qualifier("db1DataSource") DataSource db1DataSource,
@Qualifier("db2DataSource") DataSource db2DataSource) {
DataSourceTransactionManager dataSource1 = new DataSourceTransactionManager(db1DataSource);
DataSourceTransactionManager dataSource2 = new DataSourceTransactionManager(db2DataSource);
return new ChainedTransactionManager(dataSource1, dataSource2);
}
}
在使用处增加@Transactional(transactionManager = TransactionConstant.DB1_TRAN_MANAGER, rollbackFor = Exception.class)
来指定使用哪个事务
@Slf4j
@Service
@Transactional(transactionManager = TransactionConstant.DB1_TRAN_MANAGER, rollbackFor = Exception.class)
public class ExecuteServiceImpl implements IExecuteService {
@Resource
private ImageDisposeService imageDisposeService;
@Resource
private H5DataMapper h5DataMapper;
@Value("${oss.base.folder}")
private String ossBaseFolder;
@Value("${oss.target.folder}")
private String ossTargetFolder;
}
原文章地址:https://blog.csdn.net/weixin_44888446/article/details/129735504
标签:String,数据源,Plus,datasource,spring,Mybatis,new,public,dataSource From: https://www.cnblogs.com/1399z3blog/p/18040874