mybatis配置spring使用时, 原生的generator 不能满足需求 , 生成后需要修改代码, 增加和删除字段会比较烦琐, 因此需要对generator进行修改, 达到一键生成的效果.
git clone https://github.com/mybatis/generator.git
在core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal中增加一个自定义类, 继承默认注释类DefaultCommentGenerator, 对其中的方法进行重写, 达到去除烦人注释的目的.
public class ShortCommentGenerator extends DefaultCommentGenerator {
private boolean suppressAllComments;
/** If suppressAllComments is true, this option is ignored. */
private boolean addRemarkComments;
@Override
public void addConfigurationProperties(Properties properties) {
super.addConfigurationProperties(properties);
suppressAllComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
addRemarkComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
}
/**
* Adds a suitable comment to warn users that the element was generated, and when it was generated.
*
* @param xmlElement the xml element
*/
@Override
public void addComment(XmlElement xmlElement) {
}
@Override
public void addEnumComment(InnerEnum innerEnum,
IntrospectedTable introspectedTable) {
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
String remarks = introspectedColumn.getRemarks();
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
field.addJavaDocLine("/**");
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
for (String remarkLine : remarkLines) {
field.addJavaDocLine(" * " + remarkLine);
}
field.addJavaDocLine(" */");
}
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
}
@Override
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
}
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
}
在使用时, 配置注释为自定义的注释器就可以了:
修改JavaMapperGenerator 在 getCompilationUnits 函数中增加一段代码:
interfaze.addAnnotation("@Mapper");
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
为了让rootClass可以被generator访问到, 将自定义的基类复制到core/mybatis-generator-core/src/main/java/目录下(注意带上包名路径)
自定义基类可以做一些修改, 只要保留字段和其Getter/Setter就可以了, generator会做检查,如果基类中有字段了,就不会在实体类中重新添加.
示例基类:
package cn.com.xxx.common.db; //注意包名要和业务代码中一致
import java.io.Serializable;
public class BaseEntity implements Serializable {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
使用时,这样配置
因为基类继承了Serializable, 所以要增加这个变量, 主要是修改BaseRecordGenerator, 在getCompilationUnits中增加一段代码:
//...
//add serialVersionUID
Field field = new Field();
System.out.println("root class is : "+rootClass);
if(rootClass!=null){
try {
Class rootClazz=Class.forName(rootClass);
if (rootClazz.newInstance() instanceof Serializable) {
field.setType(new FullyQualifiedJavaType("long"));
field.setName("serialVersionUID");
field.setVisibility(JavaVisibility.PRIVATE);
field.setFinal(true);
field.setStatic(true);
field.setInitializationString(new Random().nextLong() + "L");
topLevelClass.addField(field);
topLevelClass.addImportedType(field.getType());
}
} catch (Exception e) {
System.out.println("can not load rootClass");
}
}
// ...
在~/code/generator/core/mybatis-generator-core目录下执行:
mvn package
完了会在当前目录/target目录下生成包: mybatis-generator-core-1.3.7-SNAPSHOT.jar
使用这个包来生成代码,就能实现想要的功能了.
https://github.com/crazyman2010/generator
http://www.mybatis.org/generator/index.html
<