配置文件
<?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:\IdeaWorkspace\mysql-connector-java-6.0.4.jar" />
<context id="autoGenerator" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释true:是: false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://rm-uf61g96k2y8v18s45o.mysql.rds.aliyuncs.com:3306/user_00"
userId="root"
password="Dlq!@#$">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
model存放位置
targetProject:当前项目下的路径
targetPackage:路径下的包名
-->
<javaModelGenerator targetPackage="com.dlq.blog.model" targetProject="src\main\java">
<!-- 是否使用schema.table生成子包 -->
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper文件存放位置 -->
<sqlMapGenerator targetPackage="com.dlq.blog.model.mapper" targetProject="src\main\java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- mapper类存放位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dlq.blog.model" targetProject="src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 表相关信息 -->
<table schema="user_00" tableName="user_00" domainObjectName="User" >
</table>
<!--<table tableName="user_00" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>-->
</context>
</generatorConfiguration>
代码
package com.dlq.blog.test;
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;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author donglq
* @date 2017/10/11 21:36
*/
public class MybatisGenerator {
public static void main(String[] args) {
try {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/test/resources/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);
} catch (Exception e) {
e.printStackTrace();
}
}
}
jar包
mysql:mysql-connector-java:5.1.25
org.mybatis.generator:mybatis-generator-core:1.3.5