环境介绍
技术栈 | springboot+mybatis-plus+mysql |
软件 | 版本 |
mysql | 8 |
IDEA | IntelliJ IDEA 2022.2.1 |
JDK | 17 |
Spring Boot | 3.1.7 |
dynamic-datasource | 3.6.1 |
mybatis-plus | 3.5.3.2 |
加入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4.1</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
application.yml文件配置
server:
port: 8007
spring:
datasource:
dynamic:
primary: sys2 #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
wms:
url: jdbc:p6spy:mysql://127.0.0.1:3306/Wms?useUnicode=true&characterEncoding=UTF-8
username: root
password: 111111
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
# driver-class-name: com.mysql.jdbc.Driver
sys2:
url: jdbc:p6spy:mysql://127.0.0.1:3306/sys?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username: root
password: 111111
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
mybatis-plus:
configuration:
#输出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置映射规则
map-underscore-to-camel-case: true #表示支持下划线到驼蜂的映射
#隐藏mybatis图标
global-config:
banner: false
db-config:
logic-delete-field: status
logic-not-delete-value: 1
logic-delete-value: 0
pagehelper:
propertyName: propertyValue
reasonable: false
defaultCount: true # 分页插件默认参数支持 default-count 形式,自定义扩展的参数,必须大小
application中添加DdlApplicationRunner
@Bean
public DdlApplicationRunner ddlApplicationRunner(@Autowired(required = false) List ddlList) {
return new DdlApplicationRunner(ddlList);
}
问题
***************************
APPLICATION FAILED TO START
***************************
Description:
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
解决办法
1、方法一
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4.1</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</exclusion>
</exclusions>
</dependency>
2、方法二
<dependency>
??? <groupId>com.baomidou</groupId>
??? <artifactId>mybatis-plus-boot-starter</artifactId>
??? <version>3.5.3</version>
??? <exclusions>
??????? <exclusion>
??????????? <artifactId>mybatis-spring</artifactId>
??????????? <groupId>org.mybatis</groupId>
??????? </exclusion>
??? </exclusions>
</dependency>
<dependency>
??? <groupId>org.mybatis</groupId>
??? <artifactId>mybatis-spring</artifactId>
??? <version>3.0.3</version>
</dependency>
3、方法三
升级dynamic-datasource-spring-boot-starter版本至3.6.1或最新版本
问题
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'ddlApplicationRunner' is expected to be of type 'org.springframework.boot.Runner' but was actually of type 'org.springframework.beans.factory.support.NullBean'
解决办法
在application中添加DdlApplicationRunner
@Bean
public DdlApplicationRunner ddlApplicationRunner(@Autowired(required = false) List ddlList) {
return new DdlApplicationRunner(ddlList);
}
https://baomidou.com/
为简化开发而生
MyBatis-Plus(opens new window)(简称 MP)是一个 MyBatis (opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。