1.文件位置(2个)
2.依赖(pom.xml)
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis逆向工程依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<build>
<plugins>
<!-- 自动生成mybatis-generator-core依赖引入(核心) -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
3.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>
<!-- 自己的oracle数据库jar包的位置 -->
<classPathEntry location="*******\oracle\ojdbc6\11.2.0.1.0\ojdbc6-11.2.0.1.0.jar" />
<context id="context1">
<property name="javaFileEncoding" value="UTF-8" />
<commentGenerator type="com.自己的目录.demo.CustomCommentGenerator" >
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 自己的oracle数据库 -->
<jdbcConnection connectionURL="jdbc:oracle:thin:@//127.0.0.1:1521/LOUTE"
driverClass="oracle.jdbc.driver.OracleDriver" password="scott"
userId="scott">
<!-- 针对oracle数据库 -->
<property name="remarksReporting" value="true"></property>
</jdbcConnection>
<!-- 配置pojo生成的位置 -->
<javaModelGenerator targetPackage="com.自己的.demo.pojo" targetProject="自己的/src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 配置sql映射文件的生成位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="自己的/src/main/resources">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</sqlMapGenerator>
<!-- 配置dao接口文件的生成位置 -->
<javaClientGenerator targetPackage="com.自己的.demo.mapper" targetProject="自己的/src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaClientGenerator>
<table tableName="自己的表名" domainObjectName="表对应的实体类名"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4.生成工具(CustomCommentGenerator)
package com.boyan.demo;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class CustomCommentGenerator extends DefaultCommentGenerator {
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// 添加字段注释
field.addJavaDocLine("/**");
if (introspectedColumn.getRemarks() != null) {
field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
}
// addJavadocTag(field, false);
field.addJavaDocLine(" */");
}
public static void main(String[] args) throws Exception {
try {
List<String> warnings = new ArrayList();
boolean overwrite = true;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
//注意文件位置,按需调整
InputStream is = classloader.getResourceAsStream("builder/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
//在文件末尾 添加异常信息输出 不在抛出异常里
if (warnings.isEmpty()) {
System.out.println("生成对应文件成功!!");
} else {
//异常打印
System.err.println(warnings);
}
// System.out.println("生成成功!");
} catch (Exception e) {
log.error("生成失败");
e.printStackTrace();
}
}
}
5.上面的代码执行后会生成3个文件,一个xml文件,一个mapper接口文件,一个实体类文件,可按需修改generatorConfig.xml生成额外的文件