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 >
<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{
List<String>warnings=new ArrayList<>();
Configuration configuration=new ConfigurationParser(warnings).
parseConfiguration(new File("src/main/resources/generatorConfig.xml" ));
DefaultShellCallback callback=new DefaultShellCallback(true );
MyBatisGenerator generator=new MyBatisGenerator(configuration, callback, warnings);
generator.generate(null );
}
}
maven依赖
<dependency >
<groupId > org.mybatis.generator</groupId >
<artifactId > mybatis-generator-core</artifactId >
</dependency >
<dependency >
<groupId > mysql</groupId >
<artifactId > mysql-connector-java</artifactId >
</dependency >