欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis Plus 代碼生成器的實現(xiàn)

 更新時間:2020年09月08日 09:45:49   作者:圣斗士Morty  
這篇文章主要介紹了Mybatis Plus 代碼生成器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

代碼生成器

MyBatis Plus是MyBatis的擴展框架,而代碼生成器是MP的核心功能之一,另外還有 “條件構(gòu)造器”和“通用CRUD”等功能。

步驟演示

mp的代碼生成器有兩種方式自動生成代碼,一種是通過main方法來執(zhí)行程序,另一種是通過maven插件build產(chǎn)生。第二種方法需要在pom.xml中添加大量的配置信息,因此本人偏向于使用第一種方式。步驟如下:

一、添加mybatis plus依賴:

如果還沒有創(chuàng)建項目,當然需要先創(chuàng)建一個工程項目,然后將jar包依賴添加到項目的classpath下,如果是含有pom.xml的maven項目,比如我最近在使用的springboot項目,那么可以直接添加pom依賴:

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

連接:maven中央庫,阿里庫

二、創(chuàng)建生成器主類

官方給出的實例連接:代碼生成器, 此處貼出我整理過后的代碼:

package com.mht.springbootmybatis.generate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * <p>
 * 代碼生成器演示
 * </p>
 */
public class MpGenerator {

  /**
   * <p>
   * MySQL 生成演示
   * </p>
   */
  public static void main(String[] args) {
    AutoGenerator mpg = new AutoGenerator();
    // 選擇 freemarker 引擎,默認 Veloctiy
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setAuthor("Mht");
    gc.setOutputDir("D://workspace/spring-boot-mybatis/src/main/java");
    gc.setFileOverride(false);// 是否覆蓋同名文件,默認是false
    gc.setActiveRecord(true);// 不需要ActiveRecord特性的請改為false
    gc.setEnableCache(false);// XML 二級緩存
    gc.setBaseResultMap(true);// XML ResultMap
    gc.setBaseColumnList(false);// XML columList
    /* 自定義文件命名,注意 %s 會自動填充表實體屬性! */
    // gc.setMapperName("%sDao");
    // gc.setXmlName("%sDao");
    // gc.setServiceName("MP%sService");
    // gc.setServiceImplName("%sServiceDiy");
    // gc.setControllerName("%sAction");
    mpg.setGlobalConfig(gc);

    // 數(shù)據(jù)源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    dsc.setTypeConvert(new MySqlTypeConvert() {
      // 自定義數(shù)據(jù)庫表字段類型轉(zhuǎn)換【可選】
      @Override
      public DbColumnType processTypeConvert(String fieldType) {
        System.out.println("轉(zhuǎn)換類型:" + fieldType);
        // 注意!!processTypeConvert 存在默認類型轉(zhuǎn)換,如果不是你要的效果請自定義返回、非如下直接返回。
        return super.processTypeConvert(fieldType);
      }
    });
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("root");
    dsc.setUrl("jdbc:mysql://localhost:3306/ease-run?useUnicode=true&characterEncoding=utf8");
    mpg.setDataSource(dsc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // strategy.setCapitalMode(true);// 全局大寫命名 ORACLE 注意
    strategy.setTablePrefix(new String[] { "user_" });// 此處可以修改為您的表前綴
    strategy.setNaming(NamingStrategy.nochange);// 表名生成策略
    strategy.setInclude(new String[] { "user" }); // 需要生成的表
    // strategy.setExclude(new String[]{"test"}); // 排除生成的表
    // 自定義實體父類
    // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
    // 自定義實體,公共字段
    // strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
    // 自定義 mapper 父類
    // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
    // 自定義 service 父類
    // strategy.setSuperServiceClass("com.baomidou.demo.TestService");
    // 自定義 service 實現(xiàn)類父類
    // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
    // 自定義 controller 父類
    // strategy.setSuperControllerClass("com.baomidou.demo.TestController");
    // 【實體】是否生成字段常量(默認 false)
    // public static final String ID = "test_id";
    // strategy.setEntityColumnConstant(true);
    // 【實體】是否為構(gòu)建者模型(默認 false)
    // public User setName(String name) {this.name = name; return this;}
    // strategy.setEntityBuilderModel(true);
    mpg.setStrategy(strategy);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.mht.springbootmybatis");
//    pc.setModuleName("test");
    mpg.setPackageInfo(pc);

    // 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無】
//    InjectionConfig cfg = new InjectionConfig() {
//      @Override
//      public void initMap() {
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
//        this.setMap(map);
//      }
//    };
//
//    // 自定義 xxList.jsp 生成
//    List<FileOutConfig> focList = new ArrayList<>();
//    focList.add(new FileOutConfig("/template/list.jsp.vm") {
//      @Override
//      public String outputFile(TableInfo tableInfo) {
//        // 自定義輸入文件名稱
//        return "D://my_" + tableInfo.getEntityName() + ".jsp";
//      }
//    });
//    cfg.setFileOutConfigList(focList);
//    mpg.setCfg(cfg);
//
//    // 調(diào)整 xml 生成目錄演示
//    focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
//      @Override
//      public String outputFile(TableInfo tableInfo) {
//        return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
//      }
//    });
//    cfg.setFileOutConfigList(focList);
//    mpg.setCfg(cfg);
//
//    // 關(guān)閉默認 xml 生成,調(diào)整生成 至 根目錄
//    TemplateConfig tc = new TemplateConfig();
//    tc.setXml(null);
//    mpg.setTemplate(tc);

    // 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內(nèi)容修改,
    // 放置自己項目的 src/main/resources/templates 目錄下, 默認名稱一下可以不配置,也可以自定義模板名稱
    // TemplateConfig tc = new TemplateConfig();
    // tc.setController("...");
    // tc.setEntity("...");
    // tc.setMapper("...");
    // tc.setXml("...");
    // tc.setService("...");
    // tc.setServiceImpl("...");
    // 如上任何一個模塊如果設(shè)置 空 OR Null 將不生成該模塊。
    // mpg.setTemplate(tc);

    // 執(zhí)行生成
    mpg.execute();

    // 打印注入設(shè)置【可無】
//    System.err.println(mpg.getCfg().getMap().get("abc"));
  }

}

文件基本可以拿過來直接用,但需要注意這兩句代碼:

gc.setOutputDir("D://workspace/spring-boot-mybatis/src/main/java");// 設(shè)置文件輸出路徑
pc.setParent("com.mht.springbootmybatis");// 父包名

下圖是我項目的路徑(執(zhí)行代碼生成器之后的項目結(jié)構(gòu)):

執(zhí)行main方法前請注意,在生成文件的時候需要有一個模板引擎的選擇,MyBatis Plus的默認模板引擎是velocity。我們可以使用freemarker。

	<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
		</dependency>

最后執(zhí)行main方法就可以了。

執(zhí)行控制臺如下:


注意,代碼生成前請確保數(shù)據(jù)庫中表已經(jīng)存在。

以上就是對mybatis plus代碼生成器的簡單使用,并不是非常詳細的應(yīng)用教程風,只是簡單的入門使用,以便日后快速查閱,后續(xù)發(fā)現(xiàn)新的值得記錄的地方會繼續(xù)更新這篇博客。歡迎各位踴躍留言!

參考文章:《MyBatis Plus代碼生成器》《MyBatis Plus學(xué)習(xí)》《mybatis-plus思維導(dǎo)圖,讓mybatis-plus不再難懂

到此這篇關(guān)于Mybatis Plus 代碼生成器的實現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis Plus 代碼生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis的動態(tài)攔截sql并修改

    MyBatis的動態(tài)攔截sql并修改

    因工作需求,需要根據(jù)用戶的數(shù)據(jù)權(quán)限,來查詢并展示相應(yīng)的數(shù)據(jù),那么就需要動態(tài)攔截sql,本文就來介紹了MyBatis的動態(tài)攔截sql并修改,感興趣的可以了解一下
    2023-11-11
  • 通過JDBC連接oracle數(shù)據(jù)庫的十大技巧

    通過JDBC連接oracle數(shù)據(jù)庫的十大技巧

    通過JDBC連接oracle數(shù)據(jù)庫的十大技巧...
    2006-12-12
  • Java如何向Word模板中插入Base64圖片和數(shù)據(jù)信息

    Java如何向Word模板中插入Base64圖片和數(shù)據(jù)信息

    這篇文章主要介紹了Java如何向Word模板中插入Base64圖片和數(shù)據(jù)信息問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot下實現(xiàn)session保持方式

    SpringBoot下實現(xiàn)session保持方式

    這篇文章主要介紹了SpringBoot下實現(xiàn)session保持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java基于Lock的生產(chǎn)者消費者模型示例

    Java基于Lock的生產(chǎn)者消費者模型示例

    這篇文章主要介紹了Java基于Lock的生產(chǎn)者消費者模型,結(jié)合實例形式分析了java基于鎖機制的生產(chǎn)者消費者模型相關(guān)實現(xiàn)與使用技巧,需要的朋友可以參考下
    2018-08-08
  • 徹底搞明白Spring中的自動裝配和Autowired注解的使用

    徹底搞明白Spring中的自動裝配和Autowired注解的使用

    這篇文章主要介紹了徹底搞明白Spring中的自動裝配和Autowired注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java中io流解析及代碼實例

    Java中io流解析及代碼實例

    這篇文章主要介紹了Java中io流解析及代碼實例,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • JVM類加載機制詳解

    JVM類加載機制詳解

    本文主要介紹了JVM類加載機制的相關(guān)知識,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • 解析SpringBoot @EnableAutoConfiguration的使用

    解析SpringBoot @EnableAutoConfiguration的使用

    這篇文章主要介紹了解析SpringBoot @EnableAutoConfiguration的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java?JSON處理庫之Gson的用法詳解

    Java?JSON處理庫之Gson的用法詳解

    Gson是Google開發(fā)的一款Java?JSON處理庫,旨在簡化Java開發(fā)人員操作JSON數(shù)據(jù)的過程,本文就來和大家簡單聊聊Gson的原理與具體使用吧
    2023-05-05

最新評論