- 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>
<!-- 指定数据库驱动的jdbc驱动jar包的位置 -->
<classPathEntry location="./mysql-connector-java-5.1.40.jar" />
<context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple" >
<!-- 生成的 Java 文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 格式化 Java 代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化 XML 代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- 配置数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" userId="root" password="123456">
</jdbcConnection>
<!-- 生成实体的位置 -->
<javaModelGenerator targetPackage="me.mizhoux.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaModelGenerator>
<!-- 生成 Mapper 接口的位置 -->
<sqlMapGenerator targetPackage="me.mizhoux.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成 Mapper XML 的位置 -->
<javaClientGenerator targetPackage="me.mizhoux.mapper" type="XMLMAPPER" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 设置数据库的表名和实体类名 -->
<table tableName="t_user" domainObjectName="User">
<!-- generatedKey用于生成生成主键的方法 -->
<generatedKey column="id" sqlStatement="SELECT LAST_INSERT_ID()"/>
</table>
</context>
</generatorConfiguration>
- addRemarkComments 这个属性,就是用来生成数据库注释用的,设置成true就会生成注释
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
-
但是使用这个方法生成出来的实体类虽然注解出来了,但是多了一堆看不懂的英文。这些内容已经写死在 DefaultCommentGenerator 中了,没有办法自定义。
-
我们可以自己写个类实现 CommentGenerator 接口,然后自定义自己想要的注释,但不做任何操作 —— 因为 DefaultCommentGenerator 本文已经存在了,为了避免混淆,就叫它SimpleCommentGenerator吧
package com.chao.util;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.Properties;
public class SimpleCommentGenerator implements CommentGenerator {
public void addConfigurationProperties(Properties properties) {
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
}
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {
}
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
}
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
}
public void addJavaFileComment(CompilationUnit compilationUnit) {
}
public void addComment(XmlElement xmlElement) {
}
public void addRootComment(XmlElement xmlElement) {
}
}
- 然后定义我们自己的注释类,MySQLCommentGenerator,继承 SimpleCommentGenerator,重写我们需要的方法:
package com.chao.util;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class MySQLCommentGenerator extends SimpleCommentGenerator {
private Properties properties;
public MySQLCommentGenerator() {
properties = new Properties();
}
public void addConfigurationProperties(Properties properties) {
// 获取自定义的 properties
this.properties.putAll(properties);
}
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
String author = properties.getProperty("author");
String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd");
SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
// 获取表注释
String remarks = introspectedTable.getRemarks();
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine(" * " + remarks);
topLevelClass.addJavaDocLine(" *");
topLevelClass.addJavaDocLine(" * @author " + author);
topLevelClass.addJavaDocLine(" * @date " + dateFormatter.format(new Date()));
topLevelClass.addJavaDocLine(" */");
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
// 获取列注释
String remarks = introspectedColumn.getRemarks();
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + remarks);
field.addJavaDocLine(" */");
}
}
- 因为我们现在要使用到我们自己自定义的 CommentGenerator ,所以我们 通过代码的方式来操作
package com.chao.util;
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;
public class Generator {
public static void main( String[] args ) throws Exception {
List<String> warnings = new ArrayList<String>();
File configFile = new File("src/main/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
- 然后配置 generatorConfig.xml设置我们自己的注释生成器(这里需要注意的是如果没有配置useInformationSchema的话,生成出来的实体类就没有类注释):
<!-- 设置 useInformationSchema 属性为 true -->
<property name="useInformationSchema" value="true" />
<?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>
<!-- 引入配置文件 -->
<properties resource="database.properties"/>
<!-- 数据库驱动-->
<classPathEntry location="C:\Users\Mr He\.m2\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 自定义注释生成器 -->
<commentGenerator type="com.chao.util.MySQLCommentGenerator">
<property name="author" value="Michael Chow"/>
<property name="dateFormat" value="yyyy/MM/dd"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
<!-- 设置 useInformationSchema 属性为 true -->
<property name="useInformationSchema" value="true" />
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.chao.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.chao.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.chao.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 相关表的配置 多表就配置多个 tableName表名 domainObjectName类名 -->
<table tableName="books" domainObjectName="Books" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
- 现在,我们运行主类 Generator,成功生成了数据库中的注释:
package com.chao.pojo;
/**
* 书本表; InnoDB free: 4096 kB
*
* @author Michael Chow
* @date 2020/10/06
*/
public class Books {
/**
* 书id
*/
private Integer bookid;
/**
* 书名
*/
private String bookname;
/**
* 数量
*/
private Integer bookcounts;
/**
* 描述
*/
private String detail;
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname == null ? null : bookname.trim();
}
public Integer getBookcounts() {
return bookcounts;
}
public void setBookcounts(Integer bookcounts) {
this.bookcounts = bookcounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}
成功的生成了实体类和字段注释~