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

MyBatis-Plus中AutoGenerator的使用案例

 更新時(shí)間:2023年05月23日 11:59:10   作者:努力的小周同學(xué)  
AutoGenerator是MyBatis-Plus的代碼生成器,通過?AutoGenerator?可以快速生成?Pojo、Mapper、?Mapper?XML、Service、Controller?等各個(gè)模塊的代碼,這篇文章主要介紹了MyBatis-Plus中AutoGenerator的詳細(xì)使用案例,需要的朋友可以參考下

AutoGenerator是什么?

AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Pojo、Mapper、 Mapper XML、Service、Controller 等各個(gè)模塊的代碼

AutoGenerator能干什么?

對(duì)于單表而言,幾乎是一個(gè)全能的工具,極大的提升了開發(fā)效率。更多的關(guān)注業(yè)務(wù)邏輯的實(shí)現(xiàn)。

怎么使用?

創(chuàng)建一個(gè)AutoGenerator項(xiàng)目

AutoGenerator本身和我們項(xiàng)目沒有關(guān)聯(lián),所以可以單獨(dú)新建為一個(gè)Project,這邊也做成Maven聚 合項(xiàng)目里的一個(gè)子項(xiàng)目

AutoGenerator本身和我們項(xiàng)目沒有關(guān)聯(lián),所以可以單獨(dú)新建為一個(gè)Project,這邊也做成Maven聚 合項(xiàng)目里的一個(gè)子項(xiàng)目

 相關(guān)依賴

        <!--web 依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis-plus 依賴-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <!--mybatis-plus 代碼生成器依賴-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <!--freemarker 依賴-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </dependency>
        <!--mysql 依賴-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

CodeGenerator工具類

package com.example.generator;
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.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.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;
/**
 * 執(zhí)行 main 方法控制臺(tái)輸入模塊表名回車自動(dòng)生成對(duì)應(yīng)項(xiàng)目目錄中
 *
 * @author zhoubin
 * @since 1.0.0
 */
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.isNotEmpty(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");
        gc.setOutputDir(projectPath + "/yunzhi-generator/src/main/java");
        //作者
        gc.setAuthor("zhousir");
        //打開輸出目錄
        gc.setOpen(false);
        //xml開啟 BaseResultMap
        gc.setBaseResultMap(true);
//xml 開啟BaseColumnList
        gc.setBaseColumnList(true);
        // 實(shí)體屬性 Swagger2 注解
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        // 數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/zhou_oa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=AsiaShanghai");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.example")
                .setEntity("pojo")
                .setMapper("mapper")
                .setService("service")
                .setServiceImpl("service.impl")
                .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 + "/yunzhi-generator/src/main/resources/mapper/"
                        + tableInfo.getEntityName() + "Mapper"
                        + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //數(shù)據(jù)庫表映射到實(shí)體的命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //數(shù)據(jù)庫表字段映射到實(shí)體的命名策略
        strategy.setColumnNaming(NamingStrategy.no_change);
        //lombok模型
        strategy.setEntityLombokModel(true);
        //生成 @RestController 控制器
        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        //表前綴
        strategy.setTablePrefix("t_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

mysql 隨便創(chuàng)建兩個(gè)表, 給幾個(gè)字段就好了

這樣就生成成功了?。?!

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

相關(guān)文章

  • SpringBoot 動(dòng)態(tài)配置Profile環(huán)境的方式

    SpringBoot 動(dòng)態(tài)配置Profile環(huán)境的方式

    這篇文章主要介紹了SpringBoot 動(dòng)態(tài)配置Profile環(huán)境的方式,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • 玩轉(zhuǎn)spring boot 快速開始(1)

    玩轉(zhuǎn)spring boot 快速開始(1)

    玩轉(zhuǎn)spring boot,快速開始spring boot學(xué)習(xí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 使用Spring初始化加載InitializingBean()方法

    使用Spring初始化加載InitializingBean()方法

    這篇文章主要介紹了使用Spring初始化加載InitializingBean()方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java實(shí)現(xiàn)LRU緩存算法的參考示例

    Java實(shí)現(xiàn)LRU緩存算法的參考示例

    這篇文章主要介紹了JAVA實(shí)現(xiàn)LRU緩存算法的參考示例,幫助大家根據(jù)需求實(shí)現(xiàn)算法,對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java-Java5.0注解全面解讀

    Java-Java5.0注解全面解讀

    這篇文章主要介紹了Java-Java5.0注解全面解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot配置SLF4J日志全過程

    SpringBoot配置SLF4J日志全過程

    這篇文章主要介紹了SpringBoot配置SLF4J日志全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java計(jì)算給定字符串中出現(xiàn)次數(shù)最多的字母和該字母出現(xiàn)次數(shù)的方法

    java計(jì)算給定字符串中出現(xiàn)次數(shù)最多的字母和該字母出現(xiàn)次數(shù)的方法

    這篇文章主要介紹了java計(jì)算給定字符串中出現(xiàn)次數(shù)最多的字母和該字母出現(xiàn)次數(shù)的方法,涉及java字符串的遍歷、轉(zhuǎn)換及運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • java使用jdbc連接數(shù)據(jù)庫簡(jiǎn)單實(shí)例

    java使用jdbc連接數(shù)據(jù)庫簡(jiǎn)單實(shí)例

    這篇文章主要為大家詳細(xì)介紹了java使用jdbc連接數(shù)據(jù)庫的簡(jiǎn)單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 圖文詳解MyEclipse更換背景主題的方法

    圖文詳解MyEclipse更換背景主題的方法

    今天小編就為大家分享一篇關(guān)于MyEclipse更換背景主題的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 解析Nacos的API居然存在這么嚴(yán)重的漏洞

    解析Nacos的API居然存在這么嚴(yán)重的漏洞

    這篇文章主要介紹了Nacos的API居然存在這么嚴(yán)重的漏洞,Nacos為我們提供了大量API,但是這些API默認(rèn)是沒有開啟認(rèn)證的,直接可以訪問,針對(duì)于這一點(diǎn)我們也都可以去驗(yàn)證一下,本文給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧
    2022-09-09

最新評(píng)論