Mybatis generator逆向工程生成代碼教程(代碼生成器)

在JavaWeb工程中,每一個(gè)SSM新項(xiàng)目或者說是SpringBoot項(xiàng)目也好,都少不了model、controller、service、dao等層次的構(gòu)建。使用Mybatis Generator為你自動(dòng)生成實(shí)體類、Mapper接口以及對(duì)應(yīng)的XML文件
可以減少你的代碼量,減少枯燥無味的重復(fù)代碼。

簡(jiǎn)介

MyBatis Generator簡(jiǎn)稱MBG,是MyBatis 官方出的代碼生成器。MBG能夠自動(dòng)生成實(shí)體類、Mapper接口以及對(duì)應(yīng)的XML文件,能夠在一定程度上減輕開發(fā)人員的工作量。本文介紹了使用MBG Maven插件的使用方法。

使用流程

  1. 數(shù)據(jù)庫(kù)根據(jù)實(shí)際需要?jiǎng)?chuàng)建表(我這里以一個(gè)項(xiàng)目為例,表創(chuàng)建過程省略,主要流程我會(huì)解釋)
  2. 引入Mybatis Generator相關(guān)插件依賴
  3. 適當(dāng)修改配置文件,配置文件已有詳細(xì)注釋,不做贅述
  4. 使用maven中的插件,點(diǎn)擊即自動(dòng)生成相關(guān)文件

一、表創(chuàng)建:

我這里就不創(chuàng)建表了,我已經(jīng)創(chuàng)建了配置文件中的幾張表。


image.png

二、依賴

主要插件依賴

          <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis.generator.version}</version>
                <configuration>
                     <!--允許移動(dòng)生成的文件-->
                    <verbose>true</verbose>
                    <!--允許覆蓋生成的文件-->
                    <overwrite>false</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate Mybatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

相關(guān)依賴:

<?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 https://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.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.selfimpr</groupId>
    <artifactId>comment</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>comment</name>
    <description>shop comment and reply</description>

    <properties>
        <java.version>1.8</java.version>
        <mybatis.generator.version>1.3.7</mybatis.generator.version>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
        <mysql.version>8.0.16</mysql.version>
    </properties>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis.generator.version}</version>
                <configuration>
                     <!--允許移動(dòng)生成的文件-->
                    <verbose>true</verbose>
                    <!--允許覆蓋生成的文件-->
                    <overwrite>false</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate Mybatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

配置

resouces目錄下generatorConfig.xml的配置如下:

<?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="system" targetRuntime="MyBatis3">
        <!-- 生成的Java文件的編碼 -->
        <property name="javaFileEncoding" value="UTF-8" />
        <commentGenerator>
            <!-- 這個(gè)元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) -->
            <!-- 如果生成日期,會(huì)造成即使修改一個(gè)字段,整個(gè)實(shí)體類所有屬性都會(huì)發(fā)生變化,不利于版本控制,所以設(shè)置為true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--數(shù)據(jù)庫(kù)驅(qū)動(dòng)、鏈接URL、用戶名、密碼 -->
        <jdbcConnection connectionURL="jdbc:mysql://localhost/comment?serverTimezone=UTC&amp;characterEncoding=utf8&amp;useSSL=false"
                        driverClass="com.mysql.cj.jdbc.Driver"
                        userId="root"
                        password="windows">

        </jdbcConnection>

        <!--  注意:不能調(diào)整標(biāo)簽順序,規(guī)定了xml表標(biāo)簽的解析順序  -->
        <!-- 生成PO類的文件配置 -->
        <javaModelGenerator targetProject=".\src\main\java"
                            targetPackage="com.selfimpr.comment.model.comment">
        </javaModelGenerator>


        <!-- 生成mapper.xml文件配置 -->
        <sqlMapGenerator targetProject=".\src\main\resources\mapper"
                         targetPackage="comment">
        </sqlMapGenerator>

        <!-- 生成Mapper.java類配置 -->
        <javaClientGenerator targetProject=".\src\main\java" type="XMLMAPPER"
                             targetPackage="com.selfimpr.comment.mapper">

        </javaClientGenerator>


        <!--  tableName對(duì)應(yīng)數(shù)據(jù)庫(kù)中表明,domainObjectName對(duì)應(yīng)生成的po類名  -->
        <!-- enableInsert 、 enableSelectByPrimaryKey等配置的作用,是否生成相應(yīng)的mapper方法和xml中的sql-->
        <table tableName="s_shop_comment" domainObjectName="Comment"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="s_shop_comment_like" domainObjectName="CommentLike"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="s_shop_comment_reference" domainObjectName="CommentReference"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="s_shop_comment_reply" domainObjectName="CommentReply"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="s_shop_comment_reply_like" domainObjectName="CommentReplyLike"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="s_shop_reply" domainObjectName="Reply"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="s_shop_reply_like" domainObjectName="ReplyLike"
               enableInsert="true" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

三、配置:

生成PO類配置
        <!--  注意:不能調(diào)整標(biāo)簽順序,規(guī)定了xml表標(biāo)簽的解析順序  -->
        <!-- 生成PO類的文件配置 -->
        <javaModelGenerator targetProject=".\src\main\java"
                            targetPackage="com.selfimpr.comment.model.comment">
        </javaModelGenerator>
PO類配置對(duì)應(yīng)的目錄
image.png
生成xxx.xml文件配置
        <!-- 生成mapper.xml文件配置 -->
        <sqlMapGenerator targetProject=".\src\main\resources\mapper"
                         targetPackage="comment">
        </sqlMapGenerator>
xxx.xml文件配置對(duì)應(yīng)目錄
image.png
生成mapper接口配置
        <!-- 生成Mapper.java類配置 -->
        <javaClientGenerator targetProject=".\src\main\java" type="XMLMAPPER"
                             targetPackage="com.selfimpr.comment.mapper">

        </javaClientGenerator>
生成mapper接口對(duì)應(yīng)目錄
image.png

四、執(zhí)行插件,生成相關(guān)代碼和文件

在maven的plugn插件中找到mybatis-generator插件,執(zhí)行g(shù)oal目標(biāo)即可生成
image.png
生成文件預(yù)覽
  • 生成的po類
image.png
  • 生成的xml文件
image.png
  • 生成的mapper接口
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容