爱分享的代码君

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

一、添加依赖

打开项目的“pom.xml”文件,把以下依赖添加到文件的“<dependencies>”节点中

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>${mybatis.plus.version}</version>
            <scope>compile</scope>
        </dependency>
        <!--3.5.9及以上版本分页必须引入以下依赖-->
<dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-jsqlparser</artifactId>
        <version>${mybatis.plus.version}</version>
        <scope>compile</scope>
    </dependency> 
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
            <scope>compile</scope>
        </dependency>
<dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

把以下内容添加到“<properties>”节点中

<mybatis.plus.version>3.5.16</mybatis.plus.version>
<druid.version>1.2.27</druid.version>

二、配置数据库连接

进入项目的“src/main/resources/application.yaml”文件

image

1.添加数据源

spring: 
  datasource:
    url: jdbc:mysql://localhost:3307/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
    username: root
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true

2.配置mybatis-plus

mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml   #mapper.xml文件扫描路径,一般放在模块的resources目录中
  typeAliasesPackage: com.framework.**.domain.vo   #**实体别名,表示多级目录,一般用于通配模块  
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: auto

三、配置拦截器

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

 

posted on 2026-03-09 19:53  爱分享的代码君  阅读(1)  评论(0)    收藏  举报