环境:intelliJ,maven,java,mysql,mybaties

1.在pom.xml中加入generator插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.配置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>

    <!-- 数据库驱动-->
    <classPathEntry location="你的路径/mysql-connector-java-8.0.20.jar" />

    <context id="mysql_blog" targetRuntime="MyBatis3">

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库URL,用户名、密码 -->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/blog_test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC"
                userId="root"
                password="root"/>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 根据精度解析,为 true时把JDBC DECIMAL 和 NUMERIC
             类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 指定entity映射文件生成的位置 -->
        <javaModelGenerator targetPackage="com.test.blog.entity" targetProject="src/main/java">
            <!-- 根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false;如果多个数据库改为true分目录 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 指定mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 指定dao映射文件生成的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.blog.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

3.配置运行插件
image.png

image.png

image.png

image.png

配置好后运行就可以了
image.png

详细解释可以参考这篇文章https://blog.csdn.net/isea533/article/details/42102297


LiuJi
7 声望0 粉丝