springboot3.4.5 配置mybatis-plus

一、引入依赖

<!--数据库相关依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.11</version>
</dependency>

<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-jsqlparser</artifactId>
     <version>3.5.11</version>
</dependency>

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.4.0</version>
</dependency>

二、配置文件yml

# server配置
server:
  port: 8080
  servlet:
    context-path: /

# 链接池配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: "password"
    url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai

# 配置mybatis-plus, 默认xml文件在 mapper/**/*.xml
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-aliases-package: com.wt.pojo
  global-config:
    db-config:
      logic-delete-field: isDeleted
      id-type: auto
      table-prefix: news_

三、配置类(方法、启动类)

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // 分页
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    // 乐观锁
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    // 防止全局删除或更新
    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
    return interceptor;
}

 四、踩坑

1、依赖引入

mybatis-plus-spring-boot3-starter 带3

mybatis-plus-jsqlparser 版本高要引入,否则分页、乐观锁、防误全删或全改报错
posted @ 2025-05-08 19:12  市丸银  阅读(909)  评论(0)    收藏  举报