mybatis-generator-config.xml在日常开发中,无论是使用的MVC架构或者是DDD架构,都少不了需要构建实体类或者其他层的情况,并且还需要自行创建Mapper.xml等文件,手动创建很麻烦而且容易出现错误

mybatis-generator-maven-plugin 插件
Mybatis的一款代码生成插件,可以快速的生成DO、DOMAIN、Mapper、XML等一系列绑定的文件,好了,废话不多说,接下来看看应如何使用

1.插件配置文件导入

我们在创建项目后,项目现在还处于空白状态,首先需要导入插件的配置文件,这个文件的内容就是我们需要生成内容的配置,比如说数据库连接,需要生成哪些类,生成的类根据哪些规则生成等,如下

mybatis-generator-config.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>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">

    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
        <property name="suppressDate" value="true"/>
    </commentGenerator>

    <!-- 数据库的连接信息 -->
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                    connectionURL="jdbc:mysql://10.58.11.252:4366/hera?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT&amp;rewriteBatchedStatements=true"
                    userId="hera"
                    password="l42v4rgd">
        <!-- 只搜寻当前库中的表-->
       <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>

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

    <!-- javaBean的生成策略-->
    <javaModelGenerator targetPackage="com.wqf.mybatis.pojo"
                        targetProject="src/main/java">
        <property name="enableSubPackages" value="true" />
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- SQL映射文件的生成策略 -->
    <sqlMapGenerator targetPackage="com.wqf.mybatis.mapper"
                     targetProject="src/main/resources">
        <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- Mapper接口的生成策略 -->
    <javaClientGenerator type="XMLMAPPER"
                         targetPackage="com.wqf.mybatis.mapper" targetProject="src/main/java">
        <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 逆向分析的表 -->
    <!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
    <!-- domainObjectName属性指定生成出来的实体类的类名 -->
    <table tableName="defect_record" domainObjectName="Defect" enableCountByExample= "false" enableUpdateByExample= "false" enableDeleteByExample= "false"
           enableSelectByExample= "false" selectByExampleQueryId= "false" />
    <table tableName="issues_record" domainObjectName="Issues" enableCountByExample= "false" enableUpdateByExample= "false" enableDeleteByExample= "false"
           enableSelectByExample= "false" selectByExampleQueryId= "false"/>
    <table tableName="requirements_record" domainObjectName="Requirement" enableCountByExample= "false" enableUpdateByExample= "false" enableDeleteByExample= "false"
           enableSelectByExample= "false" selectByExampleQueryId= "false"/>

</context>
</generatorConfiguration>

2.在pom.xml中引入插件

 <build>
    <plugins>
        <plugin>
            <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
            <configuration>
                <!--配置文件的位置-->
                <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
                <verbose>true</verbose>
                <overwrite>true</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>8.0.30</version>
                </dependency>
                <!--生成代码插件-->
                <dependency>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-core</artifactId>
                    <version>1.3.7</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

3.在idea中运行该插件

双击运行该插件
2023-12-14T09:02:25.png

最后修改:2023 年 12 月 14 日
如果觉得我的文章对你有用,请随意赞赏