Use IDEA to generate mapper and pojo based on maven project using mybatis-generator-plugin

original address


Although MyBatis is very convenient, it is still very tiring to write all the mappers by hand. Fortunately, MyBatis officially launched an automated tool, which can directly generate all the code of the DAO layer and below according to the database and the defined configuration, which is very convenient.
First wom We build a maven project by ourselves, I will not write
the configuration of mybatis-generator in detail here

Open the pom.xml file, add 3 dependencies and the mybatis-generator plug-in, namely 1. mybatis3.xjar package 2. Reverse engineering core package 3. Database connection package 4. log4j.jar for outputting logs

<build>
        <plugins>
            <plugin>
                <!--
                用maven mybatis插件
                如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
                在plugin外部得jar包,他不会去找到并执行,
                所以要把plugin运行依赖得jar配置都放在里面
                -->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>1.2.17</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                        <version>3.2.6</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.30</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Create a reverse engineering configuration file generatorConfig.xml under the src/main/resources package with the following contents:

<?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="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        password="123qwe">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
            userId="yycg"
            password="yycg">
        </jdbcConnection> -->

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

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="po"
                            targetProject="src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="mapper"
                             targetProject="src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table tableName="items"></table>
        <table tableName="orders"></table>
        <table tableName="orderdetail"></table>
        <!-- <table schema="" tableName="sys_user"></table>
        <table schema="" tableName="sys_role"></table>
        <table schema="" tableName="sys_permission"></table>
        <table schema="" tableName="sys_user_role"></table>
        <table schema="" tableName="sys_role_permission"></table> -->

    <table tableName="t_warningSetting" domainObjectName="WarningSetting" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>

Where to modify:

javaModelGenerator, the location where the PO class is generated sqlMapGenerator, the location 
where the mapper mapping file is generated 
javaClientGenerator, the location table generated by the mapper interface 
, whose tableName attribute corresponds to the corresponding table in the database

Click on the maven projects label on the right side of IDEA, and follow the picture below to 
write the picture description here

The final project directory structure after running the plugin is as follows: 
write picture description here

Application of mybatis-generator

mybatis-generator often builds a common project such as A separately, generates the corresponding mapper and po by running reverse engineering, and then copies these two packages to the web project we created using the ssm framework, rather than directly in the web project using reverse engineering.

通过运行上述的程序,我们便通过数据库中的表快速的生成了相应的po类和mapper,而不用我们程序员自己再编写相应的po类和mapper,为我们带来了很大的方便,所以这个一定要学会,在后续开发中只要使用到mybatis的地方我们都会通过mybatis的逆向工程自动为我们生成mapper和po类。


原文地址


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325720992&siteId=291194637