Mybatis 逆向工程

Mybatis逆向工程:

推荐用Java和XML Configuration的方式生成逆向文件

Java类:

package generation;

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

public class Generation {

    public void generation() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);
    }

    public static void main(String[] args) {
        Generation generation = new Generation();
        try {
            generation.generation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

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>

    <!--数据库驱动 -->
    <classPathEntry
        location="E:\JavaPack\JAR\MySQL\mysql-connector-java-5.1.28-bin.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库链接地址账号密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://172.22.144.214/HaiNan" userId="root"
            password="Abcd1234">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!--生成Model类存放位置 -->
        <javaModelGenerator targetPackage="cn.edu.hainan.model"
            targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
            
        </javaModelGenerator>
        <!--生成映射文件存放位置 -->
        <sqlMapGenerator targetPackage="cn.edu.hainan.mapper"
            targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--生成Dao类存放位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.edu.hainan.mapper" targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!--生成对应表及类名 -->
        <table tableName="company" domainObjectName="Company"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
        </table>
    </context>
    
</generatorConfiguration>

注意:在表中如果<table>标签中没有配置<property name="useActualColumnNames" value="true" />,生成的类的属性不符合驼峰命名的规则

 

posted @ 2015-10-30 04:18  googlemeoften  阅读(543)  评论(0编辑  收藏  举报