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

MyBatis Plus中代碼生成器使用詳解

 更新時(shí)間:2020年08月26日 10:16:41   作者:wshanshi  
這篇文章主要介紹了MyBatis Plus中代碼生成器使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

按照官網(wǎng)上實(shí)例嘗試了一下,感覺MyBatis plus中代碼生成器還是很強(qiáng)大的,以下是測試的總結(jié):

使用MybatisPlus的主要依賴

引入plus依賴(苞米豆)

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.1.1</version>
  </dependency>

生成器依賴

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>3.1.1</version>
 </dependency>

模板依賴

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

測試的pom依賴(也有我測試別東西的依賴,多余的請(qǐng)忽略)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.1.5.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.wy</groupId>
 <artifactId>testpuls</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>testpuls</name>
 <description>Demo project for Spring Boot</description>

 <properties>
 <java.version>1.8</java.version>
 </properties>

 <dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
 </dependency>
 <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
 </dependency>
 <dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.1.1</version>
 </dependency>
 <dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>3.1.1</version>
 </dependency>
 <dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.28</version>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>5.1.7.RELEASE</version>
  <scope>compile</scope>
 </dependency>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-test</artifactId>
 </dependency>
 <dependency>
  <groupId>com.capgemini.mrchecker</groupId>
  <artifactId>mrchecker-core-module</artifactId>
  <version>4.12.1.1</version>
 </dependency>
 <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-spring-service-connector</artifactId>
  <version>2.0.4.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>com.capgemini.mrchecker</groupId>
  <artifactId>mrchecker-core-module</artifactId>
  <version>4.12.1.1</version>
 </dependency>
 </dependencies>

 <build>
 <plugins>
  <plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
 </plugins>
 </build>

</project>

代碼生成器類

package com.wy.testpuls.util;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyGenerator {
  public static String scanner(String someThing) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("請(qǐng)輸入" + someThing + ":");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
      String sc = scanner.next();
      if (StringUtils.isNotEmpty(sc)) {
        return sc;
      }
    }
    throw new MybatisPlusException("請(qǐng)輸入正確的" + someThing + "!");
  }

  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("山石");
    gc.setOpen(false);
    mpg.setGlobalConfig(gc);

    // 數(shù)據(jù)源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("admin");
    mpg.setDataSource(dsc);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("請(qǐng)輸入你的包名"));
    pc.setParent("com.wy");//你哪個(gè)父目錄下創(chuàng)建包
    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/" + pc.getModuleName()
            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
      }
    });
    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);

    // 策略配置,數(shù)據(jù)庫表配置
    StrategyConfig strategy = new StrategyConfig();
    //數(shù)據(jù)庫表映射到實(shí)體的命名策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    //數(shù)據(jù)庫表字段映射到實(shí)體類的命名策略
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    //自定義繼承entity類,添加這一個(gè)會(huì)在生成實(shí)體類的時(shí)候繼承entity
    //strategy.setSuperEntityClass("com.wy.testCodeGenerator.entity");
    //實(shí)體是否為lombok模型
    strategy.setEntityLombokModel(true);
    //生成@RestController控制器
    strategy.setRestControllerStyle(true);
    //是否繼承controller
    // strategy.setSuperControllerClass("com.wy.testCodeGenerator.controller");
    strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
    strategy.setSuperEntityColumns("id");
    //駝峰轉(zhuǎn)連字符串
    strategy.setControllerMappingHyphenStyle(true);
    //表前綴
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
  }
}

注意:測試時(shí)輸入的表名必須和數(shù)據(jù)庫中一致,區(qū)分大小寫。

疑問:生成的實(shí)體類當(dāng)中沒有id。求解

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

相關(guān)文章

  • java中類之間的數(shù)據(jù)傳遞方式

    java中類之間的數(shù)據(jù)傳遞方式

    這篇文章主要介紹了java中類之間的數(shù)據(jù)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 如何使用BeanUtils.copyProperties進(jìn)行對(duì)象之間的屬性賦值

    如何使用BeanUtils.copyProperties進(jìn)行對(duì)象之間的屬性賦值

    這篇文章主要介紹了使用BeanUtils.copyProperties進(jìn)行對(duì)象之間的屬性賦值,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • java訪問者模式的靜態(tài)動(dòng)態(tài)及偽動(dòng)態(tài)分派徹底理解

    java訪問者模式的靜態(tài)動(dòng)態(tài)及偽動(dòng)態(tài)分派徹底理解

    這篇文章主要為大家介紹了java訪問者模式的靜態(tài)動(dòng)態(tài)及偽動(dòng)態(tài)分派徹底理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Java List Object[]轉(zhuǎn)換成List T的實(shí)例

    Java List Object[]轉(zhuǎn)換成List T的實(shí)例

    這篇文章主要介紹了Java List Object[]轉(zhuǎn)換成List T的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟(圖文)

    Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟(圖文)

    Maven是一個(gè)強(qiáng)大的項(xiàng)目管理工具,可以幫助您輕松地構(gòu)建和管理Spring Boot應(yīng)用程序,本文主要介紹了Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • springcloud 服務(wù)降級(jí)的實(shí)現(xiàn)方法

    springcloud 服務(wù)降級(jí)的實(shí)現(xiàn)方法

    這篇文章主要介紹了springcloud 服務(wù)降級(jí)的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java 如何為文件及文件夾添加權(quán)限

    java 如何為文件及文件夾添加權(quán)限

    這篇文章主要介紹了java 如何為文件及文件夾添加權(quán)限的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java泛型類型通配符和C#對(duì)比分析

    Java泛型類型通配符和C#對(duì)比分析

    下面小編就為大家?guī)硪黄狫ava泛型類型通配符和C#對(duì)比分析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • 自定義對(duì)象作為HashMap的Key問題

    自定義對(duì)象作為HashMap的Key問題

    這篇文章主要介紹了自定義對(duì)象作為HashMap的Key問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法

    SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法

    Quartz是一個(gè)定時(shí)任務(wù)框架,其他介紹網(wǎng)上也很詳盡。這里要介紹一下Quartz里的幾個(gè)非常核心的接口。通過實(shí)例代碼給大家講解SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法,感興趣的朋友一起看看吧
    2020-05-05

最新評(píng)論

区。| 兴安盟| 临沂市| 铜川市| 鞍山市| 抚远县| 沁水县| 贡山| 邢台市| 关岭| 万宁市| 岳西县| 北宁市| 陆良县| 荥经县| 大新县| 集安市| 剑阁县| 营口市| 巴南区| 衡阳县| 满洲里市| 凌源市| 泾川县| 屏山县| 河津市| 金川县| 滦南县| 湖北省| 孟村| 增城市| 禄丰县| 临城县| 嵊州市| 高陵县| 大兴区| 罗田县| 都江堰市| 新竹市| 九江县| 十堰市|