SSM Framework---Create code automatically using MyBatis Generator

MyBatis is a semi-automatic ORM framework, so the main job is to configure the Mapping mapping file, but because the handwritten mapping file is prone to errors, the MyBatis generator can be used to automatically generate entity classes, DAO interfaces and Mapping mapping files. This can save a lot of effort, just copy the generated code to the project.

There are many ways to use automatic generation, you can install plug-ins in eclipse, but I think the method that will be introduced below is very easy and the easiest. You don't need to install plug-ins, you only need to download a few jar packages and put them in one below the directory.

The files and jar packages needed to generate the code:

The above file download address: https://download.csdn.net/download/mxf_main/10382247

Among them are the jar package of the mybatis framework, the database driver jar package and the MyBatis generator jar package. The generatorConfig.xml is the file that we need to configure, the configuration is as follows

<?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.25-bin.jar"/>    
    <context id="DB2Tables"  targetRuntime="MyBatis3">    
        <commentGenerator>    
            <property name="suppressDate" value="true"/>    
            <!-- Whether to remove automatically generated comments true: yes: false: no -->    
            <property name="suppressAllComments" value="true"/>    
        </commentGenerator>    
        <!--Database link URL, username, password-->    
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="root">    
        </jdbcConnection>    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false"/>    
        </javaTypeResolver>    
        <!-- The package name and location of the generated model -->    
        <javaModelGenerator targetPackage="test.domain" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
            <property name="trimStrings" value="true"/>    
        </javaModelGenerator>    
        <!-- Generate the package name and location of the mapping file-->    
        <sqlMapGenerator targetPackage="test.mapping" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
        </sqlMapGenerator>    
        <!-- Generate DAO package name and location-->    
        <javaClientGenerator type="XMLMAPPER" targetPackage="test.IDao" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
        </javaClientGenerator>    
        <!-- The table to be generated tableName is the table name or view name in the database domainObjectName is the entity class name -->    
        <table tableName="teacher" domainObjectName="Teacher" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
    </context>    
</generatorConfiguration>    

  

When the above is done, just open the console, enter the lib directory, and execute the script:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

       

After success, go to the src directory to find the corresponding file and copy it to the project.

Guess you like

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