Use Mybatis Generator to automatically generate code and mapping files

(Reverse engineering file address: Click to open the link )

1. Mybatis is a semi-automatic ORM framework. The main job is to configure the Mapping mapping file. If it is a handwritten mapping file, it is easy to make mistakes and it is difficult to find errors, so we can use the Mybatis generator to automatically generate entity classes, Dao interfaces and Mapping. map file.

2. Two jar packages are required, and a generatorConfig.xml, src is the target folder of the file we want to generate (note that if it is not mysql, the database driver jar package is also different)


3. Open generatorConfig.xml and configure the path and table according to your own situation

<?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>
    <!--Database Driver-->
    <classPathEntry    location="mysql-connector-java-5.1.31.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--Database link address account password-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testdb" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--Generate Model class storage location-->
        <javaModelGenerator targetPackage="com.java.zxf.domain" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--Generate map file storage location-->
        <sqlMapGenerator targetPackage="com.java.zxf.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--Generate Dao class storage location-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.java.zxf.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--Generate corresponding table and class name-->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>
4. After configuration, right-click in the current directory to open the command line, enter java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
5. Success

6. Take a look at the src folder, and the corresponding files have also been successfully generated


7. End, sprinkle flowers~

Guess you like

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