一,参考MyBatis Generator官方教程:http://mybatis.org/generator/index.html
二,maven依赖(目前最新是1.3.7)
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
三,XML配置(new_generator.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>
<!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip"/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--自动生成toString方法-->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!--自动生成equals方法和hashcode方法-->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<!--自动实现Serializable接口-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/collection_project?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.collection.bean" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="mybatis\master" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.collection.mapper.master" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true"/>
<columnOverride column="DATE_FIELD" property="startDate"/>
<ignoreColumn column="FRED"/>
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR"/>
</table>-->
<!--权限相关表-->
<!-- <table schema="" tableName="sys_user"/>
<table schema="" tableName="sys_role"/>
<table schema="" tableName="sys_perm"/>
<table schema="" tableName="sys_role_perm"/>-->
<table schema="" tableName="sys_user_role"/>
</context>
</generatorConfiguration>
实体和mapper接口放在java目录下,mapper映射文件放在resources目录下
四,启动代码(NewGeneratorSqlMapUtils.java)
package com.collection.util;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Eric
* @Description: TODO
* @Date: 2019/9/17 10:51
* @Version: 1.0
**/
public class NewGeneratorSqlMapUtils {
public static void main(String[] args) throws Exception {
if(false) {//执行之后就改为false,防止多次执行出现重复的情况
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// File configFile = new File("generatorConfig.xml");
File configFile = new File("C:\\Users\\Administrator\\IdeaProjects\\collection\\src\\main\\resources\\generator\\new_generator.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);
}
}
}
五,总结
做个小小的记录,方便后续查看!