Mybatis-逆向工程实现

generatorConfig.xml(配置文件)




<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin">plugin>
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/tiantian" userId="root"
            password="123456">
        jdbcConnection>

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.tian.pojo"
            targetProject=".\src\main\java">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.tian.dao"
            targetProject=".\src\main\java">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.tian.dao" targetProject=".\src\main\java">
            
            <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{
    Listwarnings=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.generatorgroupId>
        <artifactId>mybatis-generator-coreartifactId>
dependency>

<dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
dependency>

你可能感兴趣的:(mybatis,笔记而已)