- 重写一个注释类
- 打包 安装到maven
- 设置pom
- generatorConfig
- 生成源码 mybatis-generator:generate
写一个 MyCommentGenerator 类继承 DefaultCommentGenerator 重写addFieldComment方法
程序如下
package org.improve4meaven.generator;
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 MyCommentGenerator extends DefaultCommentGenerator {
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// 添加字段注释
// 第一种 注释
// 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(" */");
// 第二种注释
StringBuffer sb = new StringBuffer();
if (introspectedColumn.getRemarks() != null)
sb.append(" //" + introspectedColumn.getRemarks());
sb.append(" --- ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString());
}
}
2、导出jar 包 并且安装到 meaven
mvn install:install-file -Dfile=MyCommentGenerator.jar -DgroupId=org.improve4meaven.generator -DartifactId=MyCommentGenerator -Dversion=1.0.SNAPSHOT -Dpackaging=jar
详细解释
3、设置pom
根据别的普通包配置即可
<dependency>
<groupId>org.improve4meaven.generator</groupId>
<artifactId>MyCommentGenerator</artifactId>
<version>1.0.SNAPSHOT</version>
</dependency>
插件依赖配置
<build>
<!-- 项目名称 -->
<finalName>TestMybatis</finalName>
<!-- 插件 -->
<plugins>
<plugin>
<!-- mybatis-generator:generate 使用的插件 -->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!--允许覆盖生成的文件 -->
<overwrite>true</overwrite>
</configuration>
<!-- 插件所依赖的包 -->
<dependencies>
<dependency>
<groupId>org.improve4meaven.generator</groupId>
<artifactId>MyCommentGenerator</artifactId>
<version>1.0.SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
4、generatorConfig配置
注意:commentGenerator type=“org.improve4meaven.generator.MyCommentGenerator”>
<?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:\mysqldriver\mysql-connector-java-5.1.26.jar" />
<context id="context1" targetRuntime="MyBatis3">
<commentGenerator type="org.improve4meaven.generator.MyCommentGenerator">
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3309/test?characterEncoding=utf8&useSSL=true&createDatabaseIfNotExist=true&serverTimezone=GMT&nullNamePatternMatchesAll=true"
userId="root" password="sadsad" >
<property name="useInformationSchema" value="true"></property>
</jdbcConnection>
<javaModelGenerator targetPackage="org.share.domain.tree.dao"
targetProject="src/main/java" />
<sqlMapGenerator targetPackage="xml" targetProject="src/main/resources" />
<javaClientGenerator targetPackage="org.share.domain.tree.mapper"
targetProject="src/main/java" type="XMLMAPPER" >
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- shema 数据库 tableName表明 -->
<table schema="test" tableName="sm_user" domainObjectName="SMUser"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
</table>
</context>
</generatorConfiguration>
5、
项目 右键–》run as --》 maven bulid --》弹出对话框 --》在goals中输入mybatis-generator:generate
或者 点击select --》选择你的mybatis插件 --》apply --》run
mybatis-generator:generate
生成三个文件,如下图
DAO 如下
package org.share.domain.tree.dao;
public class SMUser {
//主键 --- sm_user.pk_user
private Integer pkUser;
//姓名 --- sm_user.name
private String name;
public Integer getPkUser() {
return pkUser;
}
public void setPkUser(Integer pkUser) {
this.pkUser = pkUser;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
大概如此,如果有什么不懂得 可以留言