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

SpringBoot下使用MyBatis-Puls代碼生成器的方法

 更新時(shí)間:2020年10月19日 14:17:36   作者:GuessHat  
這篇文章主要介紹了SpringBoot下使用MyBatis-Puls代碼生成器的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.官方地址:

http://mybatis.plus/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B

2.數(shù)據(jù)庫(kù)結(jié)構(gòu):

在這里插入圖片描述

3.依賴(lài)導(dǎo)入

 <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
      <version>5.1.39</version>
    </dependency>
        <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generator</artifactId>
      <version>3.4.0</version>
    </dependency>

    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.30</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

配置freemarker是因?yàn)閙yBatis中默認(rèn)的引擎是freemarker,支持自定義引擎

3.目錄結(jié)構(gòu)

在這里插入圖片描述

4.官方生成器類(lèi)

CodeGenerator

public class CodeGenerator {
  /**
   * <p>
   * 讀取控制臺(tái)內(nèi)容
   * </p>
   */
  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) {
    // 代碼生成器
    AutoGenerator mpg = new AutoGenerator();

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    /**
     * 這里需要設(shè)定一下保存的地址是本項(xiàng)目下的/src/main/java
     */
    gc.setOutputDir(projectPath + "/maven1018/src/main/java");
    gc.setAuthor("XYD");
    gc.setOpen(false);
    // gc.setSwagger2(true); 實(shí)體屬性 Swagger2 注解
    mpg.setGlobalConfig(gc);

    // 數(shù)據(jù)源配置
    /**
     * 設(shè)置數(shù)據(jù)庫(kù)名稱(chēng)和數(shù)據(jù)庫(kù)賬戶(hù)密碼
     */
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/temporary?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("12345");
    mpg.setDataSource(dsc);

    // 包配置
    /**
     * 設(shè)置生成文件保存地址,模塊名為命令窗口輸入的模塊名
     */
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模塊名"));
    pc.setParent("com.baomidou.ant");
    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";

    /**
     * 這里定義的是生成xml文檔的輸出配置,存放在resource下
     */
    // 自定義輸出配置
//    List<FileOutConfig> focList = new ArrayList<>();
    // 自定義配置會(huì)被優(yōu)先輸出
//    focList.add(new FileOutConfig(templatePath) {
//      @Override
//      public String outputFile(TableInfo tableInfo) {
//        // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱(chēng)會(huì)跟著發(fā)生變化??!
//        return projectPath + "/maven1018/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) {
        // 判斷自定義文件夾是否需要?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);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);

//    strategy.setSuperEntityClass("你自己的父類(lèi)實(shí)體,沒(méi)有就不用設(shè)置!");

    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);

    // 公共父類(lèi)
//    strategy.setSuperControllerClass("你自己的父類(lèi)控制器,沒(méi)有就不用設(shè)置!");

    // 寫(xiě)于父類(lèi)中的公共字段
//    strategy.setSuperEntityColumns("id"); //注釋這行否則生成的實(shí)體類(lèi)中沒(méi)有Id變量

    strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
  }

}

5. 代碼生成后的配置

  • 默認(rèn)生成的代碼中實(shí)體類(lèi)是沒(méi)有id屬性的,在代碼生成類(lèi)中注釋掉strategy.setSuperEntityColumns("id");
  • 默認(rèn)生成的mapper對(duì)象上是沒(méi)有@Mapper注解,需要在主配置類(lèi)中加入@MapperScan注解,進(jìn)行mapper掃描
@SpringBootApplication
@MapperScan("com.example.crount.mapper")
public class Demo1018Application {

  public static void main(String[] args) {
    SpringApplication.run(Demo1018Application.class, args);
  }

}

另外自己要運(yùn)行代碼進(jìn)行數(shù)據(jù)庫(kù)訪(fǎng)問(wèn),所以application.properties中也要配置數(shù)據(jù)源

# 數(shù)據(jù)庫(kù)配置
spring.datasource.url=jdbc:mysql:///temporary?characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=12345

#連接池配置
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

6.controller開(kāi)發(fā)

注入service,修改訪(fǎng)問(wèn)的地址,寫(xiě)入訪(fǎng)問(wèn)的方法

@RestController
public class StudentController {

  @Autowired
  private IStudentService studentService;

  @GetMapping("/demo1")
  public String m1(){
    Student student = studentService.getById(3);
    return student.getSSex();
  }

}

7.生成的代碼放到主配置類(lèi)的同級(jí)目錄下,運(yùn)行代碼

在這里插入圖片描述

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

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼

    SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼

    延時(shí)隊(duì)列場(chǎng)景在我們?nèi)粘I(yè)務(wù)開(kāi)發(fā)中經(jīng)常遇到,它是一種特殊類(lèi)型的消息隊(duì)列,本文就來(lái)介紹一下SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 使用import導(dǎo)入靜態(tài)成員的方法

    使用import導(dǎo)入靜態(tài)成員的方法

    這篇文章主要介紹了淺談使用import導(dǎo)入靜態(tài)成員,需要的朋友可以參考下。
    2017-09-09
  • 聊聊java變量的初始化之后的默認(rèn)值

    聊聊java變量的初始化之后的默認(rèn)值

    這篇文章主要介紹了聊聊java變量的初始化之后的默認(rèn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Activiti7整合Springboot使用記錄

    Activiti7整合Springboot使用記錄

    這篇文章主要介紹了Activiti7+Springboot使用整合記錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • spring?boot教程之建立第一個(gè)HelloWorld

    spring?boot教程之建立第一個(gè)HelloWorld

    這篇文章主要介紹了spring?boot教程之建立第一個(gè)HelloWorld的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Java實(shí)現(xiàn)將Markdown轉(zhuǎn)換為純文本

    Java實(shí)現(xiàn)將Markdown轉(zhuǎn)換為純文本

    這篇文章主要為大家詳細(xì)介紹了兩種在 Java 中實(shí)現(xiàn) Markdown 轉(zhuǎn)純文本的主流方法,文中的示例代碼講解詳細(xì),大家可以根據(jù)需求選擇適合的方案
    2025-03-03
  • java數(shù)組輸出的實(shí)例代碼

    java數(shù)組輸出的實(shí)例代碼

    這篇文章主要介紹了java數(shù)組輸出的實(shí)例代碼,有需要的朋友可以參考一下
    2013-12-12
  • 在Spring?Boot使用Undertow服務(wù)的方法

    在Spring?Boot使用Undertow服務(wù)的方法

    Undertow是RedHAT紅帽公司開(kāi)源的產(chǎn)品,采用JAVA開(kāi)發(fā),是一款靈活,高性能的web服務(wù)器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默認(rèn)Web容器,這篇文章給大家介紹了在Spring?Boot使用Undertow服務(wù)的方法,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • 詳解Java中跳躍表的原理和實(shí)現(xiàn)

    詳解Java中跳躍表的原理和實(shí)現(xiàn)

    跳躍表(Skip list)是有序鏈表的擴(kuò)展,簡(jiǎn)稱(chēng)跳表,它在原有的有序鏈表上增加了多級(jí)索引,通過(guò)索引來(lái)實(shí)現(xiàn)快速查找,實(shí)質(zhì)上是一種可以進(jìn)行二分查找的有序鏈表。本文主要為大家介紹了跳躍表的原理和實(shí)現(xiàn),需要的可以參考一下
    2022-12-12
  • Java List集合排序?qū)崿F(xiàn)方法解析

    Java List集合排序?qū)崿F(xiàn)方法解析

    這篇文章主要介紹了Java List集合排序?qū)崿F(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

年辖:市辖区| 广灵县| 连江县| 巴东县| 莱芜市| 炎陵县| 海盐县| 兴海县| 锦州市| 南宁市| 衡阳县| 临高县| 秦皇岛市| 论坛| 柏乡县| 如东县| 朝阳市| 库车县| 淮阳县| 三明市| 额济纳旗| 松江区| 辽源市| 武威市| 平远县| 富阳市| 许昌县| 满城县| 潍坊市| 旬邑县| 江山市| 澄迈县| 祁东县| 鲁山县| 合江县| 饶阳县| 安龙县| 交城县| 平定县| 张掖市| 凯里市|