mybatis generator插件自定义注释

mybatis generator使用数据库注释生成java代码注释,配置如下:

继承DefaultCommentGenerator,或实现CommentGenerator接口,复写相关方法。

重写注释代码

类上注释

public void addFieldComment(Field field, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {}

属性注释

public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {}

get方法

public void addGetterComment(Method method, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {}

set方法

public void addSetterComment(Method method, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {}

具体用法参考DefaultCommentGenerator类中对应方法。

修改配置

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="C:\Users\nick\.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar" />
	<context id="MySQLTables" targetRuntime="MyBatis3">
		<commentGenerator type="com.putdns.plugin.DefaultCommentGenerator" >
			<property name="javaFileEncoding" value="UTF-8"/>
		</commentGenerator>
		<!-- <commentGenerator type="org.mybatis.generator.internal.DefaultCommentGenerator" />-->
		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/demo?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;"
			userId="root" password="pw" />
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<javaModelGenerator targetPackage="a.b.c"
			targetProject="model\src\main\java">
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<sqlMapGenerator targetPackage="cn.chinanurse.model"
			targetProject="model\src\main\resources">
		</sqlMapGenerator>
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.chinanurse" targetProject="model\src\main\java">
		</javaClientGenerator>
		
		<table tableName="table_name"
			domainObjectName="d.e.Obj" mapperName="d.e.Obj">
			<property name="useActualColumnNames" value="true" />
			<property name="addRemarkComments" value="true"/>
		</table>
			</context>
</generatorConfiguration>

运行

mybatis-generator eclipse plugin

https://marketplace.eclipse.org/content/mybatis-generator