Mybatis报错CannotFindDataSourceException: dynamic-datasource can not find primary datasource解决方案
问题背景
使用mybatis的时候报主数据源找不到错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource
### The error may exist in com/lanran/transactional/dao/PaymentMapper.java (best guess)
### The error may involve com.lanran.transactional.dao.PaymentMapper.insertBatchSomeColumn
### The error occurred while executing an update
### Cause: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource
解决方案
1 因为添加了mybatisplus的多源配置依赖,但是配置文件没有写是多源的相关配置,所以报错
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
- 第一种方式,如果不使用多源配置,就把这个依赖删除掉
- 第二种方式,application设置多源配置
server:
port: 40001
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
dynamic:
# 配置全局druid参数,请按需配置
druid:
initial-size: 5
max-active: 8
min-idle: 3
max-wait: 1000
validation-query: 'select 1'
datasource:
master:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
slave_1:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
作为程序员第 212 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Lyric: 前进 找寻新的记忆和空气
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110663.html