A:内部枚举类:InternalAttribute 中添加 例如:ATTR_DELETE_SELECTIVE_ID
B:增加 get方法:
public String getDeleteSelectiveId(){
return internalAttributes.get(}
C:增加set方法:
public void setDeleteSelectiveId(String s ){
internalAttributes.put(InternalAttribute.ATTR_DELETE_SELECTIVE_ID, s);
}
D: 修改 calculateXmlAttributes方法:
在其中增加: setDeleteSelectiveId("deleteSelective");设置接口方法名
A: 该类的getCompilationUnits方法中增加:addDeleteSelectiveMethod(interfaze);
B:addDeleteSelectiveMethod方法内容如下:生成规则绑定了主键生成规则,没有重新定义,如果有需要可定义自己的规则
protected void addDeleteSelectiveMethod(Interface interfaze) {
if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {C: 增加类:DeleteSelectiveMethodGenerator,定义方法完整内容。代码如下:
public class DeleteSelectiveMethodGenerator extends
AbstractJavaMapperMethodGenerator {
public DeleteSelectiveMethodGenerator() {
super();
}
@Override
public void addInterfaceElements(Interface interfaze) {
Set
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) {
}
}
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;
}
}
以上是增加自定义内容的全部步骤和代码。