Problem background
When using mybatis, an error is reported that the master data source cannot be found:
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
Solution
1. Because the multi-source configuration dependency of mybatisplus is added, but the configuration file does not write the related configuration of multi-source, an error is reported
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
Method 1: if you don’t use multi-source configuration, remove this dependency
Method 2: application set multi-source configuration
server:
port: 40001
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
dynamic:
# Configure the global druid parameter, please configure as needed
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
Read More:
- [Solved] Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
- [Solved] Failed to bind properties under ‘spring.datasource.type‘ to java.lang.Class<javax.sql.DataSource>
- [Solved] Springboot Error creating bean with name ‘dataSource’ defined in class path resource
- New Spring boot startup error Failed to auto-configure a DataSource
- Solution to java.lang.IllegalArgumentException: Property’dataSource’ is required
- [Solved] org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource‘
- com.alibaba.druid.pool.DruidDataSource error Warning: init datasource error
- [Solved] Mybatis uses the PageHelper paging plugin error: Could not find method on interface ibatis.executor.Executor named query.
- [Solved] Mybatis Error: Resources.getResourceAsStream(resource) cannot find the method
- [Solved] npm Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- [Solved] Error occurred during connect to primary: exception 3000301: Connection could not be established
- std::dynamic_pointer_cast Error: source type is not polymorphic
- Duplicate entry ‘1’ for key’PRIMARY’ (How to Solve)
- [Solved] mybatis plus Error: Invalid bound statement (not found)
- Opencv Can not Find opencv2/gpu/gpu.hpp [How to Solve]
- [Solved] Yolox Run Error: can‘t find starting number
- [Solved] Mybatis-plus Error: mybatisplus-Invalid bound statement (not found): com.integration.dao.ApiDao.getList
- Mybatis-plus calls its own method error: Invalid bound statement
- SpringBoot uses MyBatis error: Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL)
- MyBatis: Mapped Statements collection does not contain value for xxx