MENU

MyBatis Generate

May 9, 2021 • 默认分类

生成代码

pom.xml添加配置

<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
                <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.42</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

配置文件

<?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>
    <!-- 详情配置https://blog.csdn.net/zy987654zy/article/details/105434002 -->
    <context id="mysql"  targetRuntime="MyBatis3">
        <property name="javaFileEncoding" value="UTF-8"/>

        <commentGenerator>
            <!-- 指定生成的注释中是否包含生成的日期 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

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

        <!-- 指定JDBC类型和Java类型如何转换 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成model实体类文件位置 -->
        <javaModelGenerator targetPackage="com.yaliang.admin.entity"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <!-- <property name="trimStrings" value="true"/>-->
            <property name="rootClass" value="com.yaliang.admin.BaseDO"/>
        </javaModelGenerator>

        <!-- 生成mapper.xml配置文件位置 -->
        <sqlMapGenerator targetPackage="com.yaliang.admin.mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mapper接口文件位置 -->
        <javaClientGenerator targetPackage="com.yaliang.admin.dao"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 需要生成的实体类对应的表名,多个实体类复制多份该配置即可 -->
        <table tableName="user" domainObjectName="UserDO" mapperName="UserDao"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>