MyBatis 自动生成代码,mybatis-generator,SpringBoot 集成

pom 文件准备

        <!--mybatis 自动生成-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!-- mybatis-generator自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

注意其中的"configurationFile"是根据你自己放配置文件的位置而定

下面是我的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="/Users/zhouzhou/.m2/repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>
    <context id="my" targetRuntime="MyBatis3">
        <!-- 运行环境配置 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>

        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
            <!-- 时间格式设置 -->
            <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>

        </commentGenerator>

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

        <javaModelGenerator targetPackage="com.zhouzhou.pagehelper.dao.entity"
                            targetProject="src/main/java/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources/">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="com.zhouzhou.pagehelper.dao.mapper"
                             targetProject="src/main/java/" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="t_user" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="false"
               enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true"
               enableInsert="true"
        />


    </context>
</generatorConfiguration>

我的项目目录为:

在 idea 侧边工具栏,maven 处点击插件启动

OK,大功告成.

github 源码地址:  https://github.com/zjhzzhouzhou/pagehelper 

拥抱开源,共同进步

猜你喜欢

转载自blog.csdn.net/weixin_38399962/article/details/88121475