1. 程式人生 > >mybatis 逆向工程

mybatis 逆向工程

auth override dtd false log str model example project

根據實際情況添加相應的數據庫驅動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>

mybatis 逆向工程