Mybatis generator 接口增加自定义方法和sql配置

1、IntrospectedTable 修改:

  A:内部枚举类:InternalAttribute 中添加 例如:ATTR_DELETE_SELECTIVE_ID

 B:增加 get方法:

  public String getDeleteSelectiveId(){

      return  internalAttributes.get(
                InternalAttribute.ATTR_DELETE_SELECTIVE_ID);

    }

C:增加set方法:

 public void setDeleteSelectiveId(String s ){
    internalAttributes.put(InternalAttribute.ATTR_DELETE_SELECTIVE_ID, s);

    }

D: 修改 calculateXmlAttributes方法:

    在其中增加: setDeleteSelectiveId("deleteSelective");设置接口方法名

2、修改JavaMapperGenerator类,该类处理接口类生成

 A: 该类的getCompilationUnits方法中增加:addDeleteSelectiveMethod(interfaze);

 B:addDeleteSelectiveMethod方法内容如下:生成规则绑定了主键生成规则,没有重新定义,如果有需要可定义自己的规则

protected void addDeleteSelectiveMethod(Interface interfaze) {

if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
AbstractJavaMapperMethodGenerator methodGenerator = new DeleteSelectiveMethodGenerator();
initializeAndExecuteGenerator(methodGenerator, interfaze);
}
}

C: 增加类:DeleteSelectiveMethodGenerator,定义方法完整内容。代码如下:



public class DeleteSelectiveMethodGenerator extends
AbstractJavaMapperMethodGenerator {


public DeleteSelectiveMethodGenerator() {
super();
}


@Override
public void addInterfaceElements(Interface interfaze) {
Set importedTypes = new TreeSet();
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
method.setName(introspectedTable.getDeleteSelectiveId());


FullyQualifiedJavaType type = introspectedTable.getRules()
.calculateAllFieldsClass();
method.addParameter(new Parameter(type, "entiry"));

//添加自动生成注解
context.getCommentGenerator().addGeneralMethodComment(method,
introspectedTable);


addMapperAnnotations(interfaze, method);


interfaze.addImportedTypes(importedTypes);
interfaze.addMethod(method);
}


public void addMapperAnnotations(Interface interfaze, Method method) {
}

}

3、修改 XMLMapperGenerator类,该类处理sql文件生成

A: 方法getSqlMapElement中增加: addDeleteSelectiveElement(answer);
B:方法内容如下:
protected void addDeleteSelectiveElement(XmlElement parentElement) {
if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
AbstractXmlElementGenerator elementGenerator = new DeleteSelectiveElementGenerator();
initializeAndExecuteGenerator(elementGenerator, parentElement);
}
}
C: 增加DeleteSelectiveElementGenerator 类,定义xml的格式和内容。代码如下:



public class DeleteSelectiveElementGenerator extends
AbstractXmlElementGenerator {


public DeleteSelectiveElementGenerator() {
super();
}


@Override
public void addElements(XmlElement parentElement) {
XmlElement answer = new XmlElement("delete");


answer.addAttribute(new Attribute(
"id", introspectedTable.getDeleteSelectiveId()));
FullyQualifiedJavaType type = introspectedTable.getRules()
.calculateAllFieldsClass();
answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
type.getFullyQualifiedName()));


context.getCommentGenerator().addComment(answer);


StringBuilder sb = new StringBuilder();
sb.append("delete from "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
answer.addElement(new TextElement(sb.toString()));
answer.addElement(new TextElement(" where 1=1 "));
// where 条件
answer.addElement(getExampleIncludeElement());


parentElement.addElement(answer);
}


@Override
protected XmlElement getExampleIncludeElement() {
XmlElement whereElement = new XmlElement("if"); //$NON-NLS-1$
whereElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
StringBuilder sb = new StringBuilder();
for (IntrospectedColumn introspectedColumn : introspectedTable
.getAllColumns()) {
XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
sb.append(" != null"); //$NON-NLS-1$
isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
whereElement.addElement(isNotNullElement);


sb.setLength(0);
sb.append(" and ");
sb.append(MyBatis3FormattingUtilities
.getEscapedColumnName(introspectedColumn));
sb.append(" = "); //$NON-NLS-1$
sb.append(MyBatis3FormattingUtilities
.getParameterClause(introspectedColumn));


isNotNullElement.addElement(new TextElement(sb.toString()));
// whereElement.addElement(includeElement);
}


return whereElement;
}

}


以上是增加自定义内容的全部步骤和代码。

你可能感兴趣的:(Mybatis generator 接口增加自定义方法和sql配置)