MyBatis多数据源配置
最近项目需求要将原来的项目迁移整合在一个项目中启动,因此需要对配置多数据源进行考察,通过查阅资料发现苞米豆有dynamic-datasource这个项目可以实现,因此尝试自己搭建一下实现功能
引入依赖
dynamic-datasource项目地址:dynamic-datasource
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.4.2</version>
</dependency>
添加配置
配置在dynamic下进行,其中primary必须配置,用于选择默认的数据源,这里我们选择使用db1为默认数据源
spring:
datasource:
dynamic:
primary: db1
datasource:
db1:
url: jdbc:mariadb://database1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useAffectedRows=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
username: xxxx
password: xxxx
driver-class-name: org.mariadb.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
db2:
url: jdbc:mysql://database2/mini-dream?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useAffectedRows=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true
username: xxxx
password: xxxx
driver-class-name: org.mariadb.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
使用
使用主要是通过@DS注解进行配置选择数据源,并且可以在类上进行配置。如果不使用@DS注解的类则使用primary配置的默认数据源。
使用db1数据源
@Service
@Transactional(rollbackFor = Exception.class)
@DS("db1")
public class TagServiceImpl extends BaseServiceImpl<TagMapper, SystemTagsValue> implements TagService {
}
使用db2数据源
@Service
@Transactional(rollbackFor = Exception.class)
@DS("db2")
public class ProductServiceImpl extends BaseServiceImpl<ProductMapper, Product> implements ProductService {
}
分别调用service层方法得到结果
============db1数据源============
Product(id=1201788679127900161, sellerId=132513131, supplierId=1201788195130384385, productNo=P1912030000001, productName=英雄联盟, sellPrice=10.05, costPrice=9.05, days=3, validatedPeriod=Tue Dec 24 00:00:00 CST 2019, departType=1, type=1, status=2, isPlacedTop=false, placedTime=Thu Jan 01 00:00:00 CST 1970, remark=, isLock=false, createTime=Tue Dec 03 17:02:03 CST 2019, createBy=132513131, updateTime=Tue Dec 03 17:02:03 CST 2019, updateBy=, isDeleted=false)
=============db2数据源===========
SystemTagsValue(id=1202431422493986818, tagName=圣枪游侠, tagCode=TS1204, tagRule=, parentId=0, isNotLeaf=0, tagStatus=0, remark=, createTime=Thu Dec 05 03:27:08 CST 2019, createBy=353807608824172544, updateTime=Thu Dec 05 03:27:08 CST 2019, updateBy=, isDeleted=0)
使用过程中遇到的问题
问题1
- 启动时报错
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
- 解决方案
在启动类上的SpringBootApplication注解上将DruidDataSourceAutoConfigure类排除
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
问题2
- 调用接口时报错
java.lang.IllegalArgumentException: Can not set final java.lang.Class field org.apache.ibatis.binding.MapperProxy.mapperInterface to com.baomidou.mybatisplus.core.override.MybatisMapperProxy
- 解决方案
错误原因:
@Mapper
@DS("db1") // 该注解应该加载service的实现上
public interface Repository extends BaseMapper<SystemTagsValue> {
}
在Mapper层不能加注解@DS,该注解应该加载service的实现上