环境介绍

技术栈 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

加入依赖

com.baomidou

mybatis-plus-boot-starter

3.5.4.1

com.baomidou

mybatis-plus-generator

com.github.pagehelper

pagehelper-spring-boot-starter

1.4.3

org.springframework.boot

spring-boot-devtools

runtime

true

com.mysql

mysql-connector-j

runtime

com.alibaba

druid-spring-boot-starter

1.1.14

com.baomidou

dynamic-datasource-spring-boot-starter

3.6.1

p6spy

p6spy

3.9.1

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、方法一

com.baomidou

mybatis-plus-boot-starter

3.5.4.1

com.baomidou

mybatis-plus-generator

2、方法二

    com.baomidou     mybatis-plus-boot-starter     3.5.3                         mybatis-spring             org.mybatis                 org.mybatis     mybatis-spring     3.0.3

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介绍

为简化开发而生

MyBatis-Plus(opens new window)(简称 MP)是一个 MyBatis (opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

特性

无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: