Mybatis-逆向工程实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Curryth_/article/details/78459338

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>
    <context id="testTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/tiantian" userId="root"
            password="123456">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.tian.pojo"
            targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.tian.dao"
            targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.tian.dao" targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table tableName="tb_content" domainObjectName="Countent"></table>
        <table tableName="tb_content_category" domainObjectName="ContentCategory"></table>
        <table tableName="tb_item" domainObjectName="Item"></table>
        <table tableName="tb_item_cat" domainObjectName="ItemCat"></table>
        <table tableName="tb_item_desc" domainObjectName="ItemDesc"></table>
        <table tableName="tb_item_param" domainObjectName="ItemParam"></table>
        <table tableName="tb_item_param_item" domainObjectName="ItemParamItem"></table>
        <table tableName="tb_order" domainObjectName="Order"></table>
        <table tableName="tb_order_item" domainObjectName="OrderItem"></table>
        <table tableName="tb_order_shopping" domainObjectName="OrderShopping"></table>
        <table tableName="tb_user" domainObjectName="User"></table>
    </context>
</generatorConfiguration>

执行程序

package com.tian.test;

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

    public static void main(String[] args) throws Exception{
    List<String>warnings=new ArrayList<>();
    //1、获取配置文件
    Configuration configuration=new ConfigurationParser(warnings).
            parseConfiguration(new File("src/main/resources/generatorConfig.xml"));
    //2、创建回调接口的实现类
    DefaultShellCallback callback=new DefaultShellCallback(true);
    //3、创建生成器对象
    MyBatisGenerator generator=new MyBatisGenerator(configuration, callback, warnings);
    //4、生成
    generator.generate(null);
}
}

maven依赖

<!-- 逆向工程 -->
<dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
</dependency>
<!-- MySql -->
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
</dependency>

猜你喜欢

转载自blog.csdn.net/Curryth_/article/details/78459338