数据库中已经建立好了表。
第一步:
pom.xml中需要添加依赖包:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
第二步
在resources下,创建一个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 >
<classPathEntry location="F:/apache-maven-3.3.9/repo/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar" />
<context id="context1" >
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/aep" userId="root" password="root" />
<!-- 配置生成model文件路径 -->
<javaModelGenerator targetPackage="com.kylin.aep.entity" targetProject="src/main/java" />
<!-- 配置生成mapper文件路径 -->
<sqlMapGenerator targetPackage="entity" targetProject="src/main/resources" />
<!-- 配置生成dao接口文件路径 -->
<javaClientGenerator targetPackage="com.kylin.aep.dao" targetProject="src/main/java" type="XMLMAPPER" />
<!-- 配置需要自动生成代码的表 -->
<table tableName="manualscripts"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>
第三步
pom.xml中添加依赖插件:
<build>
<finalName>mybatis_generator</finalName>
<plugins>
<!-- mybatis.generator插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路径 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
第四步
执行mybatis-generator:generate命令
第五步:
刷新项目
即可看到生成的映射文件和实体类对象。
高级教程:
原版的mybatis逆向工程生成的实体类没有,本版本主要就是反编译逆向工程源码后修改一个类的方法:
核心包mybatis-generator-core-1.3.2.jar包下的org\mybatis\generator\internal包下的类:DefaultCommentGenerator
原版:
import ***
public class DefaultCommentGenerator implements CommentGenerator {
***
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
if(!this.suppressAllComments) {
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
sb.append(" * This class corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
this.addJavadocTag(innerClass, false);
innerClass.addJavaDocLine(" */");
}
}
}
修改后:
import ***
public class DefaultCommentGenerator implements CommentGenerator {
***
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if(introspectedColumn.getRemarks() != null) {
field.addJavaDocLine("/** " + introspectedColumn.getRemarks() + "*/");
}
}
}
修改这个方法后,再生成的实体类每个字段就自动带上注释了,当然前提是你的表字段得有备注。
最终结果:
public class Applicant {
/** 岗位*/
private String position;
/** 级别*/
private Integer level;
/** 备注*/
private String comment;
下面附上完整的案例git地址:Mybatis逆向工程