IDEA使用spring boot3.x集成mybatis-plus 配置,支持分页

  1. 1,创建一个 Spring Boot 项目。在 IntelliJ IDEA 中选择 File > New > Project,然后选择 Spring Initializr。
  2. 2,在 Spring Initializr 面板中,选择以下选项:
  3. 项目类型:Maven 或 Gradle,根据您的喜好选择。
  4. Spring Boot 版本:3.x。
  5. 语言:Java。
  6. 包名:根据您的喜好选择。
  7. 其他选项根据您的需求选择。

这里选的版本是3.0.4,并选上对应的mysql驱动版本

3,新件工程完毕后,添加 MyBatis-Plus 的依赖项。在 Maven中添加以下依赖项:

    
        17
        3.5.3
    

    
    
        com.baomidou
    		mybatis-plus-boot-starter
    		${mybatis-plus.version}
    

4,配置数据源。在 application.properties 或 application.yml 文件中配置您的数据源。例如,如果您使用的是 MySQL 数据库,则可以添加以下配置:


spring:
  application:
    name: weather
  profiles:
    active: dev

server:
  port: 8000
  servlet:
    context-path: /weather

--- #---------------------数据库配置---------------------------
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/weather?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
    username: root
    password: root

#mybatis
mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.xinjulang.weather.entity
  global-config:
    #数据库相关配置
    db-config:
      #主键类型
      id-type: ASSIGN_ID
    banner: false
  #原生配置
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true
    jdbc-type-for-null: 'null'
  configuration-properties:
    prefix:
    blobType: BLOB
    boolValue: TRUE

5,创建一个 MyBatis-Plus 的配置类。在您的应用程序中,创建一个名为“MybatisPlusConfig”的类。在这个类中,使用 MyBatis-Plus 的注解来配置数据源、实体扫描、分页插件等。

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 分页插件
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 乐观锁
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 防止全表更新与删除
        mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        return mybatisPlusInterceptor;
    }

}

6,创建一个数据访问对象(DAO)。创建一个数据访问对象来访问数据库,您可以使用 MyBatis-Plus 的注解和方法来定义您的 DAO 接口。然后,使用 Spring 的依赖注入来注入您的 DAO。

在您的应用程序中使用您的 DAO。在您的应用程序中,使用您的 DAO 来访问数据库。运行您的应用程序。在 IntelliJ IDEA 中,单击“运行”按钮来启动您的应用程序。您的应用程序现在应该可以使用 MyBatis-Plus 访问数据库了。

配置后的完整工程如下截图,下一篇我们开始配置代码生成

展开阅读全文

页面更新:2024-05-11

标签:数据源   注解   喜好   应用程序   实体   插件   选项   对象   版本   数据库

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top