MyBatis Generator配置使用

天風浪浪海山蒼蒼發表於2017-02-14

MyBatis Generator 是MyBatis提供生成程式碼的工具,用於生成entity、Mapper、Mapper.xml
所需jar包:mybatis-generator-core-1.3.5.jar

XML配置

<?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="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">

    <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
      <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
      <!-- caseSensitive預設false,當資料庫表名區分大小寫時,可以將該屬性設定為true -->
      <property name="caseSensitive" value="true"/>
    </plugin>

    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://localhost:3306/gsinspector"
                    userId="root"
                    password="root">
    </jdbcConnection>

    <!-- 生成實體類 -->
    <javaModelGenerator targetPackage="com.yupont.gs.model" targetProject="d:1"/>
    
    <!-- 生成mapper對應的xml檔案,可選生成 -->
    <sqlMapGenerator targetPackage="com.yupont.gs.dao.mapper"  targetProject="d:2"/>

    <!-- 生成mapper -->
    <javaClientGenerator targetPackage="com.yupont.gs.dao.mapper" targetProject="d:3" type="XMLMAPPER" />

    <!-- 指定%所有表包含一個都有一個id自增的主鍵 -->
    <!-- <table tableName="%" >
      <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
    </table> -->
    
    <!--對應資料庫表Test1,指定生成的名字為Test,例如TestMapper.java,Test.java,Test,TestMapper.xml-->
    <table tableName="Test1" domainObjectName="Test" >
    </table>
  </context>
</generatorConfiguration>

Java程式碼


import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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 org.springframework.core.io.ClassPathResource;

/**
 * MyBatis Generator
 * 用於自動生成entity,Mapper介面,Mapper xml檔案(有通用Mapper,此xml檔案可選生成)
 * 使用時需修改generatorConfig.xml檔案配置
 * 
 * @author liutianshu
 *
 */
public class Generator {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(new ClassPathResource("generatorConfig.xml").getInputStream());
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

參考資料:
http://blog.csdn.net/isea533/…
http://mbg.cndocs.tk/

相關文章