MyBatis Reverse Engineering - automatically generated model, xml mapping file, mapper interfaces according to the data table

 

MyBatis Generator (MBG) using

MBG can be generated from the data table corresponding to the model, xml mapping file, Mapper interface simply generated, but also modified as required.

 

1, download the jar package

https://github.com/mybatis/generator/releases

Unpacked jar package 3, only one of:

 

 

2. Create a new java project introduced mybatis.jar, mybatis-generator-core.jar, database-driven.

 

 

3, src under the new config.xml

http://mybatis.org/generator/configreference/xmlconfig.html

To the official website copy xml document, amended as follows:

<? xml Version = "1.0" encoding = "UTF-8"?> 
<! DOCTYPE generatorConfiguration 
        the PUBLIC "- // mybatis.org//DTD the Configuration MyBatis Generator 1.0 // EN" 
        "http://mybatis.org/dtd /mybatis-generator-config_1_0.dtd "> 

<generatorConfiguration> <-! package configuration database connection, we do not need to be commented -> 
<-! <classpathentry LOCATION =" / Program Files / the IBM / the SQLLIB /java/db2java.zip "/> -> 
    <-! context environment configuration -> 
    <context ID =" myMBG "targetRuntime =" MyBatis 3 "> <-! configured to generate annotation -> 
        <commentGenerator> 
            < ! - Note removed -> 
            <Property name = "suppressAllComments "value =" to true "/> 
            <-! removed timestamp annotation ->
    


        
            <Property name = "suppressDate" value = "to true" /> 
        </ commentGenerator>
 
        <- - database connection information!> 
        <JdbcConnection driverClass = " com.mysql.cj.jdbc.Driver " 
                        the connectionURL = " JDBC: MySQL: localhost //: 3306 / my_db serverTimezone = GMT? " 
                        userId =" CHy " 
                        password =" ABCD "> 
        </ JdbcConnection> 

        <- configuration jdbc - type conversion between the Java ->! 
        <javaTypeResolver> 
            <Property name = "forceBigDecimals" value = "false" />
        </javaTypeResolver>
 
        <!- generating a rule configuration pojo class -> 
        <javaModelGenerator targetPackage = "com.chy.model" targetProject = "the src" > 
            <Property name = "enableSubPackages" value = "to true" /> 
            <Property name = "trimStrings" value = "to true" /> 
        </ javaModelGenerator> 

        <-! map generation rules xml configuration file -> 
        <sqlMapGenerator targetPackage = "com.chy.mapper" targetProject = "the src" > 
            <Property name = "enableSubPackages" = value "to true" /> 
        </ sqlMapGenerator> 

        <-! generation rule of the mapper interface -> 
        <javaClientGenerator type = "XMLMAPPER"targetPackage="com.chy.mapper"  targetProject="src">
            <property name="enableSubPackages" value="true" /><-! table configuration data to be used, the configuration comes too cumbersome, comment out ->
        </ javaClientGenerator>

        
        <table tableName="user_tb"/>
        <table tableName="goods_tb"/>
<!--        <table schema="my_db" tableName="ALLTYPES" domainObjectName="Customer" >-->
<!--            <property name="useActualColumnNames" value="true"/>-->
<!--            <generatedKey column="ID" sqlStatement="DB2" identity="true" />-->
<!--            <columnOverride column="DATE_FIELD" property="startDate" />-->
<!--            <ignoreColumn column="FRED" />-->
<!--            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
<!--        </table>-->

    </context>
</generatorConfiguration>

Red part is to be modified. The main modification is to generate the location of the connection information database, file.

detailed configuration xml file can refer to the official document: http://mybatis.org/generator/configreference/xmlconfig.html

 

 

4, src under the new master class test.Test

http://mybatis.org/generator/running/runningWithJava.html    

To the official website to copy the code change, note the xml that, read as follows:

package test;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/config.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

Just pay attention to the xml file path is correct.

 

 

5, the main class copy operation, the generated file to be used in the project, modified as required.

 

 

Can only generate simple code, also you need to write complex, such as relational query.

File name can easily take as long as xml file to get on the path. Common generatorConfig.xml, Generator.java, find it difficult to write simple words can be used instead.

 

 

Official website: http://mybatis.org/generator/index.html

github:https://github.com/mybatis/generator

Code generator with a GUI: https://github.com/zouzg/mybatis-generator-gui

Guess you like

Origin www.cnblogs.com/chy18883701161/p/12227853.html