代码改变世界

mybatis 逆向工程

2017-08-08 10:00  甘雨路  阅读(276)  评论(0编辑  收藏  举报

根据实际情况添加相应的数据库驱动jar包

 

package util;

import java.io.File;
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;

/** 
 * @author lf 
 */
public class MyBatisGeneratorTool {

    public static void main(String[] args) {
        List<String> a = new ArrayList<String>();
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        // 配置文件的路径
        String genCfg = "/xml/generator.xml";
        File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            myBatisGenerator.generate(null);
            System.out.println("文件生成成功!!!!");
        } catch (SQLException e) {
            System.out.println("文件生成失败!!!!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件生成失败!!!!");
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("文件生成失败!!!!");
            e.printStackTrace();
        }
    }
}
<?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>
    <!-- 配置mybatis-generator-core.jar所在的目录,本地workspace目录 -->
    <classPathEntry
        location="D:\work\WorkSpaces\MyEclipse_Professional_2014\generator\lib\mybatis-generator-core-1.3.1.jar" />
    <context id="MySqlTables" targetRuntime="MyBatis3">
        <!-- <plugin
            type="com.nbesoft.framework.mybatis.util.MyBatisPaginationPlugin">
        </plugin> -->
        <!-- 关闭注释信息 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:13306/lf"
            userId="zlf" password="zlf">
        </jdbcConnection>
        
        <!-- 类型转换 -->  
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        
        <!-- model的配置 -->
        <!-- targetProject的目录配置成本地机器的workspace -->
        <javaModelGenerator targetPackage="com.mapper.model"
    targetProject="D:\work\WorkSpaces\MyEclipse_Professional_2014\generator\src\">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- mybatis的xml的配置 -->
        <sqlMapGenerator targetPackage="com.mapper.xml"
            targetProject="D:\work\WorkSpaces\MyEclipse_Professional_2014\generator\src\">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- mapper的配置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.mapper"
            targetProject="D:\work\WorkSpaces\MyEclipse_Professional_2014\generator\src\">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- tableName对应的数据库表名, domainObjectName对应程序中的实体类名 -->
        <!-- <table tableName="T_CRMSRV_CUSTINFO" domainObjectName="CustInfo"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false" >
            忽略列,不生成bean 字段  
            <ignoreColumn column="FRED" />  
            指定列的java数据类型  
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />  
        </table> -->
        
        <!-- <table tableName="ROLE" domainObjectName="Role"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false" />

        <table tableName="USER" domainObjectName="User"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false" />
        
        <table tableName="USER_ROLE" domainObjectName="UserRole"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false" /> -->
        <table tableName="BSD_USER" domainObjectName="BsdUser"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>