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

在JavaWeb工程中,每一个SSM新项目或者说是SpringBoot项目也好,都少不了model、controller、service、dao等层次的构建。使用Mybatis Generator为你自动生成实体类、Mapper接口以及对应的XML文件
可以减少你的代码量,减少枯燥无味的重复代码。

###简介
MyBatis Generator简称MBG,是MyBatis 官方出的代码生成器。MBG能够自动生成实体类、Mapper接口以及对应的XML文件,能够在一定程度上减轻开发人员的工作量。本文介绍了使用MBG Maven插件的使用方法。

使用流程

  1. 数据库根据实际需要创建表(我这里以一个项目为例,表创建过程省略,主要流程我会解释)
  2. 引入Mybatis Generator相关插件依赖
  3. 适当修改配置文件,配置文件已有详细注释,不做赘述
  4. 使用maven中的插件,点击即自动生成相关文件

一、表创建:

我这里就不创建表了,我已经创建了配置文件中的几张表。
image.png

二、依赖

主要插件依赖
          <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis.generator.version}</version>
                <configuration>
                     <!--允许移动生成的文件-->
                    <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>
相关依赖:
<?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>
                     <!--允许移动生成的文件-->
                    <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>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据库驱动、链接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>

        <!--  注意:不能调整标签顺序,规定了xml表标签的解析顺序  -->
        <!-- 生成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对应数据库中表明,domainObjectName对应生成的po类名  -->
        <!-- enableInsert 、 enableSelectByPrimaryKey等配置的作用,是否生成相应的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类配置
        <!--  注意:不能调整标签顺序,规定了xml表标签的解析顺序  -->
        <!-- 生成PO类的文件配置 -->
        <javaModelGenerator targetProject=".\src\main\java"
                            targetPackage="com.selfimpr.comment.model.comment">
        </javaModelGenerator>
PO类配置对应的目录

image.png

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

image.png

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

        </javaClientGenerator>
生成mapper接口对应目录

image.png

四、执行插件,生成相关代码和文件

在maven的plugn插件中找到mybatis-generator插件,执行goal目标即可生成

image.png

生成文件预览
  • 生成的po类

image.png

  • 生成的xml文件

image.png

  • 生成的mapper接口

image.png

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值