- 为数据库中的表A生成A.java, A.java, A.xml
- 由于该插件生成的A.java, A.xml会带有example, 不希望生成example
- 数据库中的字段写有注释, 希望注释能自动生成在A.java中
具体操作:
配置文件-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="D://apache-maven-3.3.9//public-lib//maven-dependencies//mysql//mysql-connector-java//5.1.30//mysql-connector-java-5.1.30.jar"/>
<context id="my" targetRuntime="MyBatis3">
<!-- 注释 --> <!-- type="com.util.MyDefaultCommentGenerator" 该类是我拓展的,这样才能生成数据库对应的注解 -->
<commentGenerator type="com.util.MyDefaultCommentGenerator">
<!-- 是否取消注释 如果为true只为会生成最简单的字段说明,也是建议使用的 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="false" /> <!-- 是否生成注释代时间戳-->
</commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/foo" userId="root"
password="root"/> <!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- java model 对象类 -->
<javaModelGenerator targetPackage="com.sf.fs.entity"
targetProject="MyBatis_Generator_1.3">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- sqlmap 配置文件 -->
<sqlMapGenerator targetPackage="com.sf.fs.mybatis_config"
targetProject="MyBatis_Generator_1.3">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <!-- dao 操作类 -->
<javaClientGenerator targetPackage="com.sf.fs.dao"
targetProject="MyBatis_Generator_1.3" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 由于该插件生成的A.java, A.xml会带有example, 不希望生成example 配置如下-->
<table tableName="A" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" domainObjectName="A">
</table> </context>
</generatorConfiguration>
2.自己写一个类MyDefaultCommentGenerator继承 mybatis-generator.jar 包下面org.mybatis.generator.internal.DefaultCommentGenerator ;
package com.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.internal.DefaultCommentGenerator; public class MyDefaultCommentGenerator extends DefaultCommentGenerator { public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
//1.详解方式
// // 添加字段注释
// StringBuffer sb = new StringBuffer();
//
// field.addJavaDocLine("/**");
// field.addJavaDocLine(" * <pre>");
// if (introspectedColumn.getRemarks() != null)
// field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
// sb.append(" * 表字段 : ");
// sb.append(introspectedTable.getFullyQualifiedTable());
// sb.append('.');
// sb.append(introspectedColumn.getActualColumnName());
// field.addJavaDocLine(sb.toString());
// field.addJavaDocLine(" * </pre>");
// field.addJavaDocLine(" * ");
// // addJavadocTag(field, false);
// field.addJavaDocLine(" */"); //2.简洁方式
if (introspectedColumn.getRemarks()==null) {
return;
}
field.addJavaDocLine("/** "+introspectedColumn.getRemarks()+" */"); } }
3.最后一步必须在生成的项目中添加包mybatis-generator-core.jar包 具体的版本使用当前最新的就行.