1. 程式人生 > 其它 >MyBatis Plus程式碼生成器

MyBatis Plus程式碼生成器

一、簡介

MyBatis-Plus 是一個 Mybatis 增強版工具,在 MyBatis 上擴充了其他功能沒有改變其基本功能,為了簡化開發提交效率而存在。

官網文件地址:
  https://mp.baomidou.com/guide/

MyBatis-Plus 特性:
  https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7

二、快速程式碼生成

最近團隊在推薦使用MyBatis-Plus,雖然一直知道他的存在,但由於自己手動擋的玩習慣了,不太喜歡用自動檔的東西所以一直也沒認真去看過。但為了迎合生產,在最近的專案中也聽取建議上了MyBatis-Plus,在看過官方文件後感覺這玩意也挺有意思的。所以在這次的電商平臺中也準備全程用MyBatis-Plus來寫,關於快速程式碼生成在網上也看到過很多文章,但個人還是喜歡看權威的,所以在這裡貼上網址,有興趣的可以自己看下:

https://mp.baomidou.com/guide/generator.html#使用教程

首先引用依賴

      <!--程式碼生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3
</version> </dependency>
//程式碼生成器
public class CodeGenerator {
    public static void main(String[] args) {
        // 程式碼生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全域性配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("
user.dir"); //輸出的專案路徑 gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("ljx"); gc.setOpen(false);//是否開啟 gc.setServiceImplName("%sServoce");//Server名稱規苑%sAction,想看命名規範的可以點進原始碼看他setServiceImplName的說明 // gc.setSwagger2(true); 實體屬性 Swagger2 註解 mpg.setGlobalConfig(gc); // 資料來源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/ghy_goods?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("ghy"); pc.setParent("com.ghy.springcloudgoodsservice"); 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<>(); // // 自定義配置會被優先輸出 // focList.add(new FileOutConfig(templatePath) { // @Override // public String outputFile(TableInfo tableInfo) { // // 自定義輸出檔名 , 如果你 Entity 設定了前後綴、此處注意 xml 的名稱會跟著發生變化!! // return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; // } // }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判斷自定義資料夾是否需要建立 checkDir("呼叫預設方法建立的目錄,自定義目錄用"); if (fileType == FileType.MAPPER) { // 已經生成 mapper 檔案判斷存在,不想重新生成返回 false return !new File(filePath).exists(); } // 允許生成模板檔案 return true; } }); */ // cfg.setFileOutConfigList(focList); // mpg.setCfg(cfg); // // // 配置模板 // TemplateConfig templateConfig = new TemplateConfig(); // // // 配置自定義輸出模板 // //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據使用的模板引擎自動識別 // // templateConfig.setEntity("templates/entity2.java"); // // templateConfig.setService(); // // templateConfig.setController(); // // templateConfig.setXml(null); // mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); // strategy.setSuperEntityClass("你自己的父類實體,沒有就不用設定!"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // 公共父類 // strategy.setSuperControllerClass("你自己的父類控制器,沒有就不用設定!"); // 寫於父類中的公共欄位 strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

這樣改完成後執行就可以自動生成程式碼了