MyBatis Generator配置使用

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/

    原文作者:天风浪浪海山苍苍
    原文地址: https://segmentfault.com/a/1190000008347409
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞