逆向工程

1、执行文件MyGenerator.java文件

package com.lbs.Generator;

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 MyGenerator {
    public void generate() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("G:\\ProjectCode\\workspace-sts-3.9.9.RELEASE\\WebServer\\src\\main\\java\\com\\lbs\\Generator\\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 {
            MyGenerator myGenerator = new MyGenerator();
            myGenerator.generate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

  2、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>
    <context id="MybatisGenerator" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释,true是,false否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库连接信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
            connectionURL="jdbc:oracle:thin:@IP:端口:数据库名"
            userId="用户名" password="密码">
        </jdbcConnection>

        <!-- 默认值 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成的po实体类的存放位置 -->
        <javaModelGenerator targetPackage="com.lbs.po"
            targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成的mapper映射文件(XML文件)的存放位置 -->
        <sqlMapGenerator targetPackage="com.lbs.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成的mapper接口(JAVA文件)的存放位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.lbs.dao" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 指定数据库表
        <table tableName="USERS" />    
        
    </context>
</generatorConfiguration>

3、在pom.xml文件中引入的核心包

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.5</version>
</dependency>

 

posted @ 2019-07-22 16:10  王曼曼  阅读(184)  评论(0编辑  收藏  举报