相信通过上一篇文章,你已经学会了怎么样来自动创建Entiy等,接下来,创建中文注释。
1.创建中文注释需要引包,因为我的是Mysql 所以我只引用了Mysql 其他的网上可以查到
jar包:
org.mybatis.generator
mybatis-generator-core
1.3.7
引用 mybatis-generator-core在POM.xml中
2.创建实现类 Mycommit.java
package com.text.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
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.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
public class Mycommit implements CommentGenerator{
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;
public Mycommit() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
@Override
public void addConfigurationProperties(Properties properties) {
// TODO Auto-generated method stub
}
@Override
public void addFieldComment(Field field,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn)
{
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
}
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
sb.setLength(0);
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
sb.append(" ");
sb.append(currentDateStr);
// addJavadocTag(innerClass, markAsDoNotDelete);
innerClass.addJavaDocLine(" */");
}
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
// TODO Auto-generated method stub
}
@Override
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
}
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
sb.setLength(0);
sb.append(" * @return ");
sb.append(introspectedColumn.getActualColumnName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
// addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param ");
sb.append(parm.getName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
// addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
// TODO Auto-generated method stub
}
@Override
public void addComment(XmlElement xmlElement) {
// TODO Auto-generated method stub
}
@Override
public void addRootComment(XmlElement rootElement) {
// TODO Auto-generated method stub
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
Set imports) {
// TODO Auto-generated method stub
}
}
这个步骤实现引用jar包的方法。
3.将这个类打包成jar包引入到pom.xml中如:
com.text
Mycommit
0.0.1-SNAPSHOT
4.引入到mybatis-generator依赖包中:
org.apache.maven.plugins
maven-compiler-plugin
1.8
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
com.text
A
0.0.1-SNAPSHOT
到此为止,所有的配置已完毕,如果在ecplise中使用,则右击工程,maven build,添加命令mybatis-generator:generate,代码生成完毕。
效果:
package com.text.entitys;
import java.math.BigDecimal;
public class Appr {
/**
*
*/
private Long id;
/**
*
*/
private String title;
/**
* 自定义服务类型
*/
private String customServiceType;
/**
* 大事件的感恩中使用,
*/
private Integer serviceType;
/**
* 大事件的感恩中使用,单位:分钟
....
* @param id
*/
public void setId(Long id) {
this.id = id;
}
/**
*
* @return title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
*/
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
/**
* 自定义服务类型
* @return custom_service_type 自定义服务类型
*/
public String getCustomServiceType() {
return customServiceType;
}
/**
百度云链接:https://pan.baidu.com/s/1uB_CqtuVw7-oKwh3c0azdw 密码:cndy(ssm完美框架以及自动生成类注释的包和文件)