使用java启动mybatis generator

Posted An

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用java启动mybatis generator相关的知识,希望对你有一定的参考价值。

1、java工程目录结构:

|src/main/java

  |com.leslie.mybatis.generator

        |MybatisGeneratorUtil.java

|src/main/resources

  generatorConfig.xml

|lib

  |mybatis-3.3.0.jar

  |mybatis-generator-core-1.3.2.jar

  |mysql-connector-java-5.1.14-bin.jar

2、MybatisGeneratorUtil.java

package com.leslie.mybatis.generator;

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;

public class MybatisGeneratorUtil {
    public static void main(String[] args) {
        try {
            System.out.println("start generator ...");
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File(MybatisGeneratorUtil.class.getResource("/generatorConfig.xml").getFile());
            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);
            System.out.println("end generator!");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3、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>
    
    <classPathEntry location="D:\svn\MyBatisGenerator\lib\mysql-connector-java-5.1.14-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://192.168.0.39:3306/leslie?useUnicode=true&amp;characterEncoding=utf-8" userId="leslie" password="123456" >
        </jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.wsj.ygbh.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.wsj.ygbh.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.wsj.ygbh.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        
        
        <table tableName="leslie_ad" domainObjectName="Ad" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="leslie_ad_position" domainObjectName="AdPosition" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        
        
    </context>
</generatorConfiguration>

 

以上是关于使用java启动mybatis generator的主要内容,如果未能解决你的问题,请参考以下文章

Java MyBatis Generator使用generator自动生成Dao,Mapping和实体文件

13.4 使用Java程序运行MyBatis Generator(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》

13.4 使用Java程序运行MyBatis Generator(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》

mybatis generator(mybatis逆向工程)使用

Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解

Mybatis generator 生成domain字段带数据库注释