天天看點

Generator自動生成配置

Generator自動生成配置

generatorConfig.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 >

    <!-- 引入配置檔案 -->  

    <!--<properties resource="db.properties"/>-->

    <context id="store" targetRuntime="MyBatis3">

        <!-- 資料源配置 -->

        <commentGenerator>

            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->

            <property name="suppressAllComments" value="true" />

            <!-- 是否去除所有自動生成的檔案的時間戳,預設為false -->

            <!-- <property name="suppressDate" value="false"/> -->

        </commentGenerator>

        <!--資料庫連接配接的資訊:驅動類、連接配接位址、使用者名、密碼 -->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                        connectionURL="jdbc:mysql://192.168.1.11:3306/test"

                        userId="root"

                        password="root">

        </jdbcConnection>

        <!-- 實體類 -->

        <!--targetPackage="com.wxc.entity.park"寫到包名,不用到java檔案 -->

        <javaModelGenerator targetPackage="com.wxc.entity.park" targetProject=".\src">

            <!-- enableSubPackages:是否讓schema作為包的字尾 -->

            <property name="enableSubPackages" value="false" />

            <!-- 從資料庫傳回的值被清理前後的空格  -->

            <property name="trimStrings" value="true" />

        </javaModelGenerator>

        <!-- mapper.xml -->

        <sqlMapGenerator targetPackage="mybatis.park" targetProject=".\resources">

            <property name="enableSubPackages" value="false" />

        </sqlMapGenerator>

        <!-- dao層-mapper接口 -->

        <javaClientGenerator targetPackage="com.wxc.dao.park" targetProject=".\src" type="XMLMAPPER">

            <property name="enableSubPackages" value="false" />

          </javaClientGenerator>

        <!-- 資料庫表名 -->

        <table tableName="park"

               enableCountByExample="false"

               enableUpdateByExample="false"

               enableDeleteByExample="false"

               enableSelectByExample="false"

               selectByExampleQueryId="false">

        </table>

    </context>

</generatorConfiguration>

GeneratorSqlmap.java

package com.wxc.util;

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 GeneratorSqlmap {

    public void generator() throws Exception{

        List<String> warnings = new ArrayList<String>();

        boolean overwrite = true;

        File configFile = new File("resources/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) throws Exception {

        try {

            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();

            generatorSqlmap.generator();

            System.out.println("成功");

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

繼續閱讀