国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

自動(dòng)化生成代碼:MyBatis 的 Generator與MyBatis-Plus 的 AutoGenerator

這篇具有很好參考價(jià)值的文章主要介紹了自動(dòng)化生成代碼:MyBatis 的 Generator與MyBatis-Plus 的 AutoGenerator。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

自動(dòng)化生成代碼是現(xiàn)在一種非常常見的技術(shù),它可以大大提高開發(fā)效率,減少重復(fù)勞動(dòng)。而在 Java 開發(fā)中,MyBatis 是一個(gè)非常流行的 ORM 框架,而其中的 Generator 和 MyBatis-Plus 中的 AutoGenerator 是兩個(gè)非常好用的自動(dòng)化代碼生成工具,下面我們來分別介紹一下它們的使用。

Mybatis Generator自動(dòng)化生成代碼

MyBatis Generator概述

MyBatis Generator 是 MyBatis 框架提供的一個(gè)自動(dòng)生成代碼的工具,它能夠根據(jù)數(shù)據(jù)庫中的表自動(dòng)生成對(duì)應(yīng)的 POJO、Mapper 接口和 XML 配置文件,同時(shí)也支持自定義插件的開發(fā)。使用 MyBatis Generator 的步驟如下:

使用Java代碼形式

1. 在 Maven 或 Gradle 中添加 MyBatis Generator 的依賴:

<dependency>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-core</artifactId>
  <version>1.4.0</version>
</dependency>

2. 編寫配置文件 GeneratorConfig.xml,配置需要生成的數(shù)據(jù)庫表和對(duì)應(yīng)的生成器:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/test"
            userId="root"
            password="root" />

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.example.pojo"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.example.mapper"
            targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.example.mapper"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="tb_user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />

    </context>
</generatorConfiguration>

3. 在命令行中使用 MyBatis Generator 進(jìn)行代碼生成:

java -jar mybatis-generator-core-1.4.0.jar -configfile GeneratorConfig.xml -overwrite

這樣就會(huì)在指定的包路徑和項(xiàng)目路徑下生成對(duì)應(yīng)的 POJO、Mapper 接口和 XML 配置文件。但編寫代碼還需要配置一些信息,也挺麻煩哈,偷個(gè)懶吧再,使用Maven 插件幫咱們干活。

使用Maven插件

pom.xml中添加依賴

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

pom.xml中build-plugins下添加插件

添加了插件后,我們使用 configurationFile 元素來指定一個(gè)配置文件 mybatis-generator-config.xml
而且數(shù)據(jù)庫表可能會(huì)發(fā)生變動(dòng),因此我們需要追加一個(gè)配置 <overwrite>true</overwrite>,允許覆蓋舊的文件。為了防止我們編寫的 SQL 語句被覆蓋掉,MyBatis Generator 只會(huì)覆蓋舊的 po、dao、而 *mapper.xml 不會(huì)覆蓋,而是追加。

<!-- MyBatis Generator 插件 -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
        <!-- MyBatis Generator 生成器的配置文件-->
        <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
        <!-- 允許覆蓋生成的文件,確定骨架代碼后就可以設(shè)為 false 了,免得覆蓋原有代碼 -->
        <overwrite>true</overwrite>
        <!-- 將當(dāng)前 pom 的依賴項(xiàng)添加到生成器的類路徑中-->
        <includeCompileDependencies>true</includeCompileDependencies>
    </configuration>
</plugin>

結(jié)構(gòu)如下圖:
mybatis 代碼自動(dòng)生成,Mybatis,自動(dòng)化,mybatis

mybatis-generator-config.xml

<generatorConfiguration>
    <context id="myContext" targetRuntime="MyBatis3" defaultModelType="flat">

        <!-- 注釋 -->
        <commentGenerator>
            <!-- 是否不生成注釋 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- jdbc連接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false"
                        userId="root"
                        password="1234">
        </jdbcConnection>

        <!-- 類型轉(zhuǎn)換 -->
        <javaTypeResolver>
            <!--是否使用bigDecimal,默認(rèn)false。
                false:把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer
                true:把JDBC DECIMAL 和 NUMERIC 類型解析為java.math.BigDecimal-->
            <property name="forceBigDecimals" value="true"/>
        </javaTypeResolver>

        <!-- 生成實(shí)體類地址 -->
        <javaModelGenerator targetPackage="com.example.pojo" targetProject="src/main/java">
            <!-- 是否針對(duì)string類型的字段在set方法中進(jìn)行修剪,默認(rèn)false -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>


        <!-- 生成Mapper.xml文件 -->
        <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources">
        </sqlMapGenerator>

        <!-- 生成 XxxMapper.java 接口-->
        <javaClientGenerator targetPackage="com.example.mapper" targetProject="src/main/java" type="XMLMAPPER">
        <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <!-- schema為數(shù)據(jù)庫名,oracle需要配置,mysql不需要配置。
             tableName為對(duì)應(yīng)的數(shù)據(jù)庫表名
             domainObjectName 是要生成的實(shí)體類名(可以不指定,默認(rèn)按帕斯卡命名法將表名轉(zhuǎn)換成類名)
             enableXXXByExample 默認(rèn)為 true, 為 true 會(huì)生成一個(gè)對(duì)應(yīng)Example幫助類,幫助你進(jìn)行條件查詢,不想要可以設(shè)為false
             -->
        <table schema="" tableName="posts" domainObjectName="Posts"
               enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false" selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

運(yùn)行

mybatis 代碼自動(dòng)生成,Mybatis,自動(dòng)化,mybatis

MyBatis-Plus 的 AutoGenerator

MyBatis-Plus AutoGenerator概述

MyBatis-Plus 是在 MyBatis 的基礎(chǔ)上擴(kuò)展了一些功能的框架,其中 AutoGenerator 就是 MyBatis-Plus 提供的自動(dòng)生成代碼的工具,它能夠一鍵生成對(duì)應(yīng)的 POJO、Mapper 接口和 XML 配置文件,并且還支持模板引擎的自定義。

使用 MyBatis-Plus AutoGenerator 的步驟如下:

1. 在 Maven 或 Gradle 中添加 MyBatis-Plus 的依賴:

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

2. 配置數(shù)據(jù)源和 MyBatis-Plus 的相關(guān)配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.pojo
  global-config:
    db-config:
      id-type: auto

3. 編寫配置文件 MybatisPlusConfig.java,配置自動(dòng)生成代碼的相關(guān)信息:

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
        return plusProperties -> plusProperties.getGlobalConfig().setBanner(false);
    }

    @Bean
    public AutoGenerator autoGenerator(DataSource dataSource) {
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setDataSource(dataSource);

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("mybatis-plus");
        globalConfig.setFileOverride(true);
        globalConfig.setOpen(false);
        globalConfig.setEntityName("%sDO");
        autoGenerator.setGlobalConfig(globalConfig);

        // 數(shù)據(jù)庫表配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setInclude("tb_user");

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.example");
        packageConfig.setEntity("pojo");
        packageConfig.setMapper("mapper");
        packageConfig.setXml("mapper");

        // 模板引擎配置
        TemplateConfig templateConfig = new TemplateConfig();

        // 自定義模板配置,可以根據(jù)自己的需求進(jìn)行修改
        templateConfig.setService("/templates/service.vm");
        templateConfig.setServiceImpl("/templates/serviceImpl.vm");
        templateConfig.setEntity("/templates/entity.vm");
        templateConfig.setMapper("/templates/mapper.vm");
        templateConfig.setXml("/templates/mapperXml.vm");

        autoGenerator.setTemplate(templateConfig);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.setStrategy(strategyConfig);

        return autoGenerator;
    }
}

4. 在啟動(dòng)類中調(diào)用 AutoGenerator 的 run 方法即可進(jìn)行代碼生成:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        AutoGenerator autoGenerator = (AutoGenerator) ApplicationContextUtils.getBean("autoGenerator");
        autoGenerator.execute();
    }
}

這樣就可以在指定的包路徑和項(xiàng)目路徑下生成對(duì)應(yīng)的 POJO、Mapper 接口和 XML 配置文件。

兩者對(duì)比

維度 MyBatis Generator MyBatis-Plus AutoGenerator
依賴配置 需要添加 MyBatis Generator 的單獨(dú)依賴 需要添加 MyBatis-Plus 的整體依賴
配置文件 需要編寫 GeneratorConfig.xml 配置文件 不需要額外的配置文件
支持?jǐn)?shù)據(jù)庫 支持主流的關(guān)系型數(shù)據(jù)庫(如 MySQL、Oracle 等) 支持主流的關(guān)系型數(shù)據(jù)庫(如 MySQL、Oracle 等)
可生成內(nèi)容 POJO、Mapper 接口和 XML 配置文件 POJO、Mapper 接口和 XML 配置文件
插件支持 支持自定義插件開發(fā) 支持使用 MyBatis-Plus 內(nèi)置的插件
模板引擎支持 不支持模板引擎 支持使用模板引擎進(jìn)行自定義
配置靈活性 配置項(xiàng)較多,靈活度高 配置項(xiàng)較少,但使用起來更加簡(jiǎn)便
兼容性 對(duì)于 MyBatis 的版本兼容性較好 需要與 MyBatis-Plus 版本配套使用
社區(qū)支持和文檔資料數(shù) 社區(qū)支持較好,文檔資料豐富 社區(qū)支持較好,但文檔資料數(shù)目相對(duì)較少

綜上所述,MyBatis Generator 和 MyBatis-Plus AutoGenerator 都是非常好用的自動(dòng)化代碼生成工具,根據(jù)項(xiàng)目需求的不同,我們可以選擇適合自己的工具來進(jìn)行開發(fā)。MyBatis Generator 配置靈活度較高,可以根據(jù)需要進(jìn)行自定義插件的開發(fā),但需要編寫較多的配置文件,而 MyBatis-Plus AutoGenerator 則更加簡(jiǎn)便,支持模板引擎的自定義,但配置項(xiàng)較少。

總結(jié)

以上就是 MyBatis Generator 和 MyBatis-Plus AutoGenerator 兩個(gè)自動(dòng)化代碼生成工具的使用方法和區(qū)別,它們可以大大提升開發(fā)效率,減少重復(fù)勞動(dòng)。在實(shí)際開發(fā)中,我們可以根據(jù)項(xiàng)目的需求選擇合適的工具進(jìn)行使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-774317.html

到了這里,關(guān)于自動(dòng)化生成代碼:MyBatis 的 Generator與MyBatis-Plus 的 AutoGenerator的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【重要】springboot實(shí)戰(zhàn)(六)之mybatis-plus代碼自動(dòng)生成器

    【重要】springboot實(shí)戰(zhàn)(六)之mybatis-plus代碼自動(dòng)生成器

    目錄 環(huán)境: 步驟: 1.添加依賴 2.配置代碼 3.運(yùn)行 測(cè)試 1.測(cè)試生成的service 1.1、service用法 2.分頁查詢 2.1、分頁插件配置? 2.2、測(cè)試 3.源碼 jdk:1.8 springboot版本:2.7.15 mybatis-plus版本:3.5.1以上 (本文章用的當(dāng)前最新版本:3.5.3.2,代碼適用于3.5.1版本以上的版本) 在測(cè)試類中創(chuàng)建

    2024年02月03日
    瀏覽(21)
  • 【企業(yè)級(jí)SpringBoot單體項(xiàng)目模板 】——Mybatis-plus自動(dòng)代碼生成

    【企業(yè)級(jí)SpringBoot單體項(xiàng)目模板 】——Mybatis-plus自動(dòng)代碼生成

    ?? 作 ? ??????? 者 :是江迪呀 ?? 本文 : SpringBoot項(xiàng)目模版 、 企業(yè)級(jí) 、 模版 ?? 每日?? 一言 : 我們之所以這樣認(rèn)為,是因?yàn)樗麄冞@樣說。他們之所以那樣說,是因?yàn)樗麄兿胱屛覀兡菢诱J(rèn)為。所以實(shí)踐才是檢驗(yàn)真理的唯一準(zhǔn)則。 上回 我們說了一些開發(fā)規(guī)范

    2024年02月07日
    瀏覽(29)
  • Mybatis三劍客(一)在springboot中自動(dòng)生成Mybatis【generator】

    Mybatis三劍客(一)在springboot中自動(dòng)生成Mybatis【generator】

    1、pom.xml中新增plugin 2、新建resources/generatorConfig.xml 注意: ????????context 中內(nèi)容是有順序的 ????????commentGenerator在前面s 3、idea中的terminal中執(zhí)行mybatis自動(dòng)生成命令? mvn mybatis-generator:generate

    2024年02月12日
    瀏覽(22)
  • Mybatis generator實(shí)戰(zhàn):自動(dòng)生成POJO類完整解決方案

    Mybatis generator實(shí)戰(zhàn):自動(dòng)生成POJO類完整解決方案

    在用Mybatis generator 生成可以用來訪問(多個(gè))表的基礎(chǔ)對(duì)象,遇到一個(gè)問題,就是columnRenamingRule可以替換所有表元素里字段前綴 但是如果想去掉所有表的前綴,比如有多個(gè)表: 期望得到的POJO結(jié)果是: 參照: https://github.com/mybatis/generator/issues/275 https://github.com/mybatis/generator/

    2024年02月04日
    瀏覽(21)
  • 代碼生成器-mybatis-plus-generator

    代碼生成器-mybatis-plus-generator

    我們平時(shí)在開發(fā)的過程中,對(duì)于新建的一張表難免會(huì)有對(duì)其進(jìn)行增刪改查的操作,而且還要寫Controller、service、Mapper、Mapper.xml、PO、VO等等。如果每次都要去寫這些跟業(yè)務(wù)毫不相干但是卻又耗時(shí)耗力的重復(fù)代碼這不僅是讓開發(fā)人員不能專注于業(yè)務(wù)邏輯甚至可能由于不注意導(dǎo)致字

    2023年04月25日
    瀏覽(21)
  • 5.6 Mybatis代碼生成器Mybatis Generator (MBG)實(shí)戰(zhàn)詳解

    5.6 Mybatis代碼生成器Mybatis Generator (MBG)實(shí)戰(zhàn)詳解

    本文我們主要實(shí)戰(zhàn)Mybatis官方的代碼生成器:Mybatis Generator(MBG),掌握它以后,可以簡(jiǎn)化大部分手寫代碼,我們只需要寫復(fù)雜邏輯代碼! 通過前幾篇,我們掌握了在SpringBoot下Mybatis的基本用法,操作步驟回顧一下: 創(chuàng)建與MySQL表對(duì)應(yīng)的Java PO對(duì)象,字段一一對(duì)應(yīng); 創(chuàng)建Mapper接口,

    2024年02月05日
    瀏覽(20)
  • 【sgCreateAPI】自定義小工具:敏捷開發(fā)→自動(dòng)化生成API接口腳本(接口代碼生成工具)

    【sgCreateAPI】自定義小工具:敏捷開發(fā)→自動(dòng)化生成API接口腳本(接口代碼生成工具)

    ? 具體步驟:登錄 Apifox https://app.apifox.com/ ? 圈選復(fù)制上面的內(nèi)容粘貼到【接口地址列表】輸入框,自動(dòng)生成腳本代碼 生成的接口請(qǐng)求代碼是基于 【Vue.js最新版】【基于jQuery Ajax】[sd.js]最新原生完整版for凱哥API版本_你摯愛的強(qiáng)哥的博客-CSDN博客 【代碼】【最新版】【基于j

    2024年02月09日
    瀏覽(32)
  • mybatis-generator代碼生成器的使用與配置

    mybatis-generator代碼生成器的使用與配置

    官網(wǎng)的MyBatis Generator使用介紹,請(qǐng)點(diǎn)擊下面的鏈接: 鏈接 MyBatis Generator 生成的文件包含三類: (1)Model實(shí)體文件,一個(gè)數(shù)據(jù)庫表對(duì)應(yīng)生成一個(gè) Model 實(shí)體; (2)Mapper接口文件,數(shù)據(jù)數(shù)操作方法都在此接口中定義; (3)Mapper?XML配置文件 在pom.xml文件添加如下依賴: 代碼如下

    2024年02月14日
    瀏覽(17)
  • springboot+maven插件調(diào)用mybatis generator自動(dòng)生成對(duì)應(yīng)的mybatis.xml文件和java類

    springboot+maven插件調(diào)用mybatis generator自動(dòng)生成對(duì)應(yīng)的mybatis.xml文件和java類

    mybatis最繁瑣的事就是sql語句和實(shí)體類,sql語句寫在java文件里很難看,字段多的表一開始寫感覺阻力很大,沒有耐心,自動(dòng)生成便成了最稱心的做法。自動(dòng)生成xml文件,dao接口,實(shí)體類,雖一直感覺不太優(yōu)雅,但省去了很多麻煩,當(dāng)表增加或修改字段的時(shí)候重新生成便輕松搞

    2024年02月14日
    瀏覽(25)
  • mybatis-plus-generator-ui 可視化代碼生成器!

    mybatis-plus-generator-ui 可視化代碼生成器!

    它提供交互式的Web UI用于生成兼容mybatis-plus框架的相關(guān)功能代碼,包括Entity,Mapper,Mapper.xml,Service,Controller等。 可以自定義模板以及各類輸出參數(shù),也可通過SQL查詢語句直接生成代碼。 git地址 :https://github.com/davidfantasy/mybatis-plus-generator-ui 1、引入依賴 2、新建程序入口,以main函

    2024年02月08日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包