使用mybatis-generator插件生成mapper类

Posted gsls200808

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用mybatis-generator插件生成mapper类相关的知识,希望对你有一定的参考价值。

使用mybatis-generator生成mapper可以方便我们根据表创建mapper类。简化mybatis的编码

pom.xml添加plugin

 <!--mybatis-generator插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <configurationFile>src/main/resources/mybatis/generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.18</version>
                    </dependency>
                </dependencies>
            </plugin>

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="my" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>


        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.1.11/share?useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true&amp;serverTimezone=Asia/Shanghai" userId="root"
                        password="">
            <!-- 设置为只生成当前数据库的表信息 -->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <javaModelGenerator targetPackage="cn.net.iatc.fengdian.vo.mybatis" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

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

        <javaClientGenerator targetPackage="cn.net.iatc.fengdian.mapper"
                             targetProject="./src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>



        <table tableName="user_role" domainObjectName="UserRole"
               enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>

    </context>
</generatorConfiguration>

如果项目里使用了通用mapper(tk mapper)

需要使用更高版本来支持通用mapper插件

plugin添加

 <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>$basedir/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.16</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.2.1</version>
                    </dependency>
                </dependencies>
            </plugin>

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="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="javaFileEncoding" value="UTF-8"/>
        <!--配置是否使用通用 Mapper 自带的注释扩展,默认 true-->
        <!--<property name="useMapperCommentGenerator" value="false"/>-->

        <!--通用 Mapper 插件,可以生成带注解的实体类-->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <property name="caseSensitive" value="true"/>
            <!-- lombok Data注解 -->
            <property name="lombok" value="Data"/>
        </plugin>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.1.12:3306/share"
                        userId="root"
                        password="">
            <!-- 设置为只生成当前数据库的表信息 -->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <!--MyBatis 生成器只需要生成 Model-->
        <javaModelGenerator targetPackage="com.vvvtimes.usercenter.model" targetProject="./src/main/java"/>

        <!--mapper.xml-->
        <sqlMapGenerator targetPackage="mybatis.mapper.usercenter" targetProject="./src/main/resources">
        </sqlMapGenerator>

        <!--mapper.java-->
        <javaClientGenerator targetPackage="com.vvvtimes.usercenter.mapper"
                             targetProject="./src/main/java" type="XMLMAPPER">

        </javaClientGenerator>

        <table tableName="user">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

以上是关于使用mybatis-generator插件生成mapper类的主要内容,如果未能解决你的问题,请参考以下文章

使用mybatis-generator插件生成mapper类

mybatis-generator 代码自动生成插件

mybatis-generator自动生成代码插件详解

如何在idea中使用Mybatis-generator插件快速生成代码

如何在idea中使用Mybatis-generator插件快速生成代码

使用maven的mybatis-generator代码生成器插件生成实体类mapper配置文件和mapper接口(使用idea)