你好,欢迎进入江苏优软数字科技有限公司官网!

诚信、勤奋、创新、卓越

友好定价、专业客服支持、正版软件一站式服务提供

13262879759

工作日:9:00-22:00

IDEA使用spring boot3.x集成mybatis-plus 配置

发布时间:2023-06-08

浏览次数:0

1.创建一个Boot工程。 在IDEA中选择File>New>,然后选择。 2. 在面板中,选择以下选项: 项目类型:Maven 或者,根据您的喜好进行选择。 引导版本:3.x。 语言:Java。 包名:根据您的喜好选择。 其他选项根据您的需要选择。

intellij idea 下载_intellij idea er图_intellij idea 数据库关系图

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

intellij idea 下载_intellij idea er图_intellij idea 数据库关系图

3.新建工程完成后intellij idea 数据库关系图,添加-Plus的依赖。 在 Maven 中添加如下依赖:

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

4.配置数据源。 在 . 或 .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.创建一个-Plus配置类。 在您的应用程序中,创建一个名为“”的类。 在这个类中,使用-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)。 要创建一个数据访问对象来访问数据库,您可以使用 -Plus 注释和方法来定义您的 DAO 套接字。 之后,使用依赖注入来注入你的 DAO。

在您的应用程序中使用您的 DAO。 在您的应用程序中intellij idea 数据库关系图,使用您的 DAO 访问数据库。 运行您的应用程序。 在 IDEA 中,单击“运行”按钮以启动您的应用程序。 您的应用程序现在应该能够使用 -Plus 访问数据库。

配置后的完整项目如下图所示。 在下一篇文章中,我们将开始配置代码生成。

intellij idea er图_intellij idea 下载_intellij idea 数据库关系图

如有侵权请联系删除!

13262879759

微信二维码