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

mybatis-plus使用generator實現(xiàn)逆向工程

 更新時間:2022年05月09日 09:55:19   作者:菜鳥菜  
mybatis-plus-generator在3.5.0以及以后的版本使用新的方式逆向生成代碼,本文主要介紹了mybatis-plus使用generator實現(xiàn)逆向工程,具有一定的參考價值,感興趣的可以了解一下

1.背景

可以使用mybatis-plus-generator逆向生成dao層、service層、controller層等代碼

2.引入jar包

mybatis-plus-generator在3.5.0以及以后的版本使用新的方式逆向生成代碼。

這里介紹使用舊版本的方式生成代碼。

    <!-- mybatis-plus begin -->
    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
    </dependency>
    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
    </dependency>
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
    </dependency>

    <!-- mybatis-plus 默認是vm引擎 -->
    <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
    </dependency>
    <!-- mybatis-plus end -->

3.自動生成代碼

    public static void main(String[] args) {
        // 構(gòu)建一個代碼生成器對象
        AutoGenerator mpg = new AutoGenerator();
        // 配置執(zhí)行策略
        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        // 當(dāng)前項目路徑
        String proPath = System.getProperty("user.dir");
        // 設(shè)置代碼生成路徑
        gc.setOutputDir(proPath + "/src/main/java");
        // 生成的類的注釋中作者信息
        gc.setAuthor("curry");
        // 生成后是否打開文件夾
        gc.setOpen(false);
        // 是否覆蓋
        gc.setFileOverride(true);
        // 生成service類的后綴
        gc.setServiceName("%sService");
        // 主鍵生成策略 和數(shù)據(jù)庫id生成策略一致
        gc.setIdType(IdType.AUTO);
        // 設(shè)置日期類型
        gc.setDateType(DateType.ONLY_DATE);
        // 是否生成Swagger
        gc.setSwagger2(false);
        // 生成entity類的后綴
        gc.setEntityName("%sEntity");
        mpg.setGlobalConfig(gc);

        // 2.設(shè)置數(shù)據(jù)源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test_db");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 3.配置生成包的路徑
        PackageConfig pc = new PackageConfig();
        // 設(shè)置模塊存放位置
        pc.setParent("com.");
        // 設(shè)置該模塊包的路徑
        pc.setModuleName("dao");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        // 4.策略配置
        StrategyConfig strategy=new StrategyConfig();
        // 設(shè)置要映射的表名 不配置默認處理全部表
        // strategy.setInclude("user");
        // 表名中下劃線轉(zhuǎn)駝峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 表中字段如果有下劃線,轉(zhuǎn)駝峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // strategy.setEntityLombokModel(true);//自動生成Lombok
        // strategy.setRestControllerStyle(true);//開啟 RestFul 風(fēng)格
        // strategy.setControllerMappingHyphenStyle(true);
        // 對表中的字段 設(shè)置邏輯刪除 生成的dao層代碼會添加@TableLogic
        strategy.setLogicDeleteFieldName("delete_flag");

        // 5.自動填充 (表中如果有創(chuàng)建時間、修改時間話,可以使用自動填充)
        TableFill createTime = new TableFill("created_date", FieldFill.INSERT);
        TableFill updateTime = new TableFill("modified_date", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        // 樂觀鎖配置
        // strategy.setVersionFieldName("version");
        mpg.setStrategy(strategy);

        // 6.配置實體類模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 如果setXxxxx(null) 不會生成Xxxx實體類相關(guān)代碼
        // 因此如果只生成dao層代碼
        // 可以在這里控制
        templateConfig.setController(null);
        templateConfig.setMapper(null);
        templateConfig.setService(null);
        templateConfig.setServiceImpl(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 7.執(zhí)行代碼生成操作
        mpg.execute();
    }

4.修改*Mapper.xml文件的生成位置

4.1 默認*Mapper.xml文件生成位置

    // 3.配置生成包的路徑
    PackageConfig pc = new PackageConfig();
    // 設(shè)置模塊存放位置
    pc.setParent("com.");
    // 設(shè)置該模塊包的路徑
    pc.setModuleName("dao");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    mpg.setPackageInfo(pc);

以上面代碼為例,*Mapper.xml文件位置是mapper/xml/*Mapper.xml

4.2 修改*Mapper.xml文件生成位置

step1:在模板中控制不生成xml文件 防止重復(fù)生成

templateConfig.setXml(null);

step2:在第三步中mpg.execute();執(zhí)行前增加以下代碼

    // 自定義配置
    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<>();
    // 自定義配置會被優(yōu)先輸出
    // 這里自定義配置的是*Mapper.xml文件
    // 所以templatePath = "/templates/mapper.xml.vm";
    // 如果你想自定義配置其它 修改templatePath即可
    focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                    // 自定義輸出文件名 如果你 Entity 設(shè)置了前后綴
                    String entityName = tableInfo.getEntityName();
                    int length = entityName.length();
                    entityName = entityName.substring(0, length - 6);
                    return proPath + "/src/main/resources/mapper/" + 
                            entityName + "Mapper" + StringPool.DOT_XML;
            }
    });

    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

到此這篇關(guān)于mybatis-plus使用generator實現(xiàn)逆向工程的文章就介紹到這了,更多相關(guān)mybatis-plus generator 逆向工程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?AOP實現(xiàn)聲明式事務(wù)機制源碼解析

    Spring?AOP實現(xiàn)聲明式事務(wù)機制源碼解析

    這篇文章主要為大家介紹了Spring?AOP實現(xiàn)聲明式事務(wù)機制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 詳解spring mvc 請求轉(zhuǎn)發(fā)和重定向

    詳解spring mvc 請求轉(zhuǎn)發(fā)和重定向

    這篇文章主要介紹了詳解spring mvc 請求轉(zhuǎn)發(fā)和重定向,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • java實現(xiàn)文本框和文本區(qū)的輸入輸出

    java實現(xiàn)文本框和文本區(qū)的輸入輸出

    這篇文章主要介紹了java實現(xiàn)文本框和文本區(qū)的輸入輸出的方法和具體示例,有需要的小伙伴可以參考下。
    2015-06-06
  • 解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機的情況

    解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機的情況

    這篇文章主要介紹了解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機的情況,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件【測試無誤】

    SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件【測試無誤】

    這篇文章主要介紹了SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件的相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05
  • idea重新下載已刪除的maven依賴包操作

    idea重新下載已刪除的maven依賴包操作

    這篇文章主要介紹了idea重新下載已刪除的maven依賴包操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • SpringCloud開啟session共享并存儲到Redis的實現(xiàn)

    SpringCloud開啟session共享并存儲到Redis的實現(xiàn)

    這篇文章主要介紹了SpringCloud開啟session共享并存儲到Redis的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 最新評論