日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

mybatis-plus代碼生成器使用(generator)

作者:魚跡 更新時間: 2023-12-24 編程語言

mybatis-plus generator

  • 1、引入依賴
  • 2、編寫代碼生成器配置類
  • 3、可能遇到的問題

本文主要記錄在 springboot項目中使用最新版 mybatis plus generator代碼生成。

1、引入依賴

首先在springboot項目中引入mybatis plus、mybatis plus generator的依賴,如下所示:

<!--   mybatis plus     -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <!--   mybatis plus  generator   -->
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>

2、編寫代碼生成器配置類

對于新版的mybatis plus generator,官方提供了兩種代碼生成方式,一種是交互式生成,另一種是快速生成,我這里使用的是快速生成,具體代碼如下所示:

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class MybatisPlusGenerator  {
    public static void main(String[] args) {
    FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/fruit_mall_db?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai","root","123456!")
            // 全局配置
            .globalConfig((scanner, builder) -> builder.author(scanner.apply("請輸入作者名稱?")).fileOverride())
            // 包配置
            .packageConfig((scanner, builder) -> builder.parent(scanner.apply("請輸入包名?")))
            // 策略配置
            .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("請輸入表名,多個英文逗號分隔?所有輸入 all")))
                    .controllerBuilder().enableRestStyle().enableHyphenStyle()
                    .entityBuilder().enableLombok().addTableFills(
                            new Column("create_time", FieldFill.INSERT)
                    ).build())
            /*
                模板引擎配置,默認 Velocity 可選模板引擎 Beetl 或 Freemarker
               .templateEngine(new BeetlTemplateEngine())
               .templateEngine(new FreemarkerTemplateEngine())
             */
            .execute();

    }
    // 處理 all 情況
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }


}

運行后需要在控制臺輸入作者名、包名、以及數據庫表名,生成代碼后會打開生成代碼所在文件夾。上面只是一個簡單的使用方式。對于配置類具體的配置則可以查看官方的代碼生成器配置文檔,點擊跳轉

3、可能遇到的問題

如果遇到Caused by: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed錯誤,則可點擊跳轉,參考下面的文章:
該錯誤解決方法文章

我的博客即將同步至騰訊云開發者社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=g5pfeju46q72

原文鏈接:https://blog.csdn.net/weixin_45915647/article/details/130446699

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新