最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot整合mybatis-plus代碼生成器的配置解析

 更新時(shí)間:2021年02月26日 09:27:14   作者:heromps  
這篇文章主要介紹了springboot整合mybatis-plus代碼生成器的配置解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個(gè)模塊的代碼,極大的提升了開發(fā)效率。
具體實(shí)實(shí)現(xiàn)以及配置解析如下:

package mybatis_plus;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class CodeGenerator {
  public static String scanner(String tip) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("請(qǐng)輸入" + tip + ":");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
      String ipt = scanner.next();
      if (StringUtils.isNotBlank(ipt)) {
        return ipt;
      }
    }
    throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!");
  }
  public static void main(String[] args) {
    // 構(gòu)建代碼生成器
    AutoGenerator mpg = new AutoGenerator();
    //配置策略

    //1.全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("heroMps");
    gc.setOpen(false);
    gc.setFileOverride(false); //是否覆蓋
    gc.setServiceName("%sService");//去除Service前面的I
    gc.setIdType(IdType.ID_WORKER);
    gc.setDateType(DateType.ONLY_DATE);
//    gc.setSwagger2(true); //實(shí)體屬性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    //2.設(shè)置數(shù)據(jù)源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("root");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    //3.包配置
    PackageConfig pc = new PackageConfig();
//    pc.setModuleName("blog");
    pc.setParent("mybatis_plus");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    mpg.setPackageInfo(pc);
    // 自定義配置
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {
        // to do nothing
      }
    };

    // 如果模板引擎是 freemarker
//    String templatePath = "/templates/mapper.xml.ftl";
//     如果模板引擎是 velocity
     String templatePath = "/templates/mapper.xml.vm";

    // 自定義輸出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定義配置會(huì)被優(yōu)先輸出
    focList.add(new FileOutConfig(templatePath) {
      @Override
      public String outputFile(TableInfo tableInfo) {
        // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化?。?
        return projectPath + "/src/main/resources/mapper/"
            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
      }
    });
    /*
    cfg.setFileCreate(new IFileCreate() {
      @Override
      public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
        // 判斷自定義文件夾是否需要?jiǎng)?chuàng)建
        checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄,自定義目錄用");
        if (fileType == FileType.MAPPER) {
          // 已經(jīng)生成 mapper 文件判斷存在,不想重新生成返回 false
          return !new File(filePath).exists();
        }
        // 允許生成模板文件
        return true;
      }
    });
    */
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();

    // 配置自定義輸出模板
    //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會(huì)根據(jù)使用的模板引擎自動(dòng)識(shí)別
    // templateConfig.setEntity("templates/entity2.java");
    // templateConfig.setService();
    // templateConfig.setController();

    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    //4.策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true);
    strategy.setLogicDeleteFieldName("deleted");
    //自動(dòng)填充設(shè)置
    TableFill create_time = new TableFill("create_time", FieldFill.INSERT);
    TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);
    ArrayList<TableFill> list = new ArrayList<>();
    list.add(create_time);
    list.add(update_time);
    strategy.setTableFillList(list);
    //5.樂觀鎖配置
    strategy.setVersionFieldName("version");
    strategy.setRestControllerStyle(true);
    strategy.setControllerMappingHyphenStyle(true);
    mpg.setStrategy(strategy);

    mpg.execute();
  }
}

生成目錄如下:

在這里插入圖片描述

測(cè)試查詢

在這里插入圖片描述

到此這篇關(guān)于springboot整合mybatis-plus代碼生成器的文章就介紹到這了,更多相關(guān)springboot整合mybatis-plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的三種代理模式詳解

    Java中的三種代理模式詳解

    這篇文章主要介紹了Java中的三種代理模式詳解,代理模式的關(guān)鍵點(diǎn)是:代理對(duì)象與目標(biāo)對(duì)象.代理對(duì)象是對(duì)目標(biāo)對(duì)象的擴(kuò)展,并會(huì)調(diào)用目標(biāo)對(duì)象,文中提供了部分代碼,需要的朋友可以參考下
    2023-08-08
  • Java語言實(shí)現(xiàn)快速冪取模算法詳解

    Java語言實(shí)現(xiàn)快速冪取模算法詳解

    這篇文章主要介紹了Java語言實(shí)現(xiàn)快速冪取模算法詳解,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔

    Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔

    這篇文章主要介紹了Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔,幫助大家更好的理解和使用Spring Boot框架,感興趣的朋友可以了解下
    2020-10-10
  • Java線程中賣火車票問題的深入講解

    Java線程中賣火車票問題的深入講解

    這篇文章主要給大家介紹了關(guān)于Java線程中賣火車票問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • JDK版本修改不生效的解決方法

    JDK版本修改不生效的解決方法

    本文主要介紹了在配置新電腦環(huán)境時(shí)遇到JDK版本切換失敗的問題,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • 基于Java?利用Mybatis實(shí)現(xiàn)oracle批量插入及分頁查詢

    基于Java?利用Mybatis實(shí)現(xiàn)oracle批量插入及分頁查詢

    這篇文章主要介紹了基于Java?利用Mybatis實(shí)現(xiàn)oracle批量插入及分頁查詢,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-07-07
  • Java中常用的設(shè)計(jì)模式之策略模式詳解

    Java中常用的設(shè)計(jì)模式之策略模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java中常用的設(shè)計(jì)模式之策略模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • List轉(zhuǎn)變?yōu)槎禾?hào)分隔的String(Java7和Java8分別實(shí)現(xiàn))

    List轉(zhuǎn)變?yōu)槎禾?hào)分隔的String(Java7和Java8分別實(shí)現(xiàn))

    這篇文章主要介紹了Java7和Java8分別實(shí)現(xiàn)List轉(zhuǎn)變?yōu)槎禾?hào)分隔的String,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 為什么阿里要慎重使用ArrayList中的subList方法

    為什么阿里要慎重使用ArrayList中的subList方法

    這篇文章主要介紹了為什么要慎重使用ArrayList中的subList方法,subList是List接口中定義的一個(gè)方法,該方法主要用于返回一個(gè)集合中的一段、可以理解為截取一個(gè)集合中的部分元素,他的返回值也是一個(gè)List。,需要的朋友可以參考下
    2019-06-06
  • java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

    java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

    這篇文章主要為大家詳細(xì)介紹了java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評(píng)論

陆河县| 绥中县| 唐山市| 阿合奇县| 博乐市| 红原县| 苍南县| 时尚| 常德市| 体育| 孟津县| 青海省| 宜君县| 望城县| 留坝县| 大同县| 开封市| 金堂县| 都匀市| 临夏市| 张北县| 长汀县| 兴义市| 广南县| 密云县| 秦皇岛市| 灌南县| 安仁县| 马公市| 吉木萨尔县| 常德市| 田阳县| 云阳县| 孟州市| 宜昌市| 青田县| 额尔古纳市| 道孚县| 博爱县| 辽宁省| 商都县|