mybatis automatically generated mapper.xml file, model file

1. First, we create a new file genneratorConfigMysql.xml, the inside of the database, user name, database tables are replaced accordingly,

domainObjectName to generate the model file name
tableName database table names
<?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="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginingDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <commentGenerator>
            <property name="supressDate" value="true"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://xxxx?characterEncoding=utf8" userId="xxxx"
            password="xxxx" >
        </jdbcConnection>
        <javaModelGenerator targetPackage="test.qa.model" targetProject="src/main/java">
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources/"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="test.ke.dao" targetProject="src/main/java"/>
        <table tableName="xxxx"  domainObjectName="xxxx"/>
    </context>
</generatorConfiguration>
View Code

2. Create a new java file, as shown below, to generate a corresponding operation mapper.xml document model file need to generate another database table, as long as the replacement of database tables genneratorConfigMysql.xml file and the file to be generated model name to

package test.util;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成model文件和xml文件。 需要修改generatorConfigMySql.xml文件中的表和数据库
 */
public class Generator {
    public static void main(String[] args) throws Exception{
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        InputStream is = Generator.class.getResourceAsStream("/generatorConfigMySql.xml");
        if(is == null){
            System.out.println("null");
        }
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(is);
        is.close();
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        for(String warning : warnings){
            System.out.println(warning);
        }

    }
}

  

Guess you like

Origin www.cnblogs.com/leavescy/p/11240445.html