工欲善其事,必先利其器
在使用MyBatis Generator 的時候生成的XML的Mapper文件總是不盡人意,例如在處理JdbcType為VARCHAR類型是只做null判斷,在Update的時候就會出現一些不符合要求的作用不會做空字符串判斷,利用剩余時間研究了一下源代碼,如下:
package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.ListUtilities; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; import org.mybatis.generator.config.GeneratedKey; /** * * @author Jeff Butler * */ public class InsertSelectiveElementGenerator extends AbstractXmlElementGenerator { @Override public void addElements(XmlElement parentElement) { ... sb.setLength(0); sb.append(introspectedColumn.getJavaProperty()); sb.append(" != null"); //$NON-NLS-1$ XmlElement insertNotNullElement = new XmlElement("if"); //$NON-NLS-1$ insertNotNullElement.addAttribute(new Attribute( "test", sb.toString())); //$NON-NLS-1$ sb.setLength(0); sb.append(MyBatis3FormattingUtilities .getEscapedColumnName(introspectedColumn)); sb.append(','); ... }
沒看錯就是拼接字符串,在這個地方可以隨意的添加內容。
其他內容可以在
package org.mybatis.generator.codegen.ibatis2.sqlmap; public class SqlMapGenerator extends AbstractXmlGenerator { protected XmlElement getSqlMapElement() { .... addResultMapWithoutBLOBsElement(answer); addResultMapWithBLOBsElement(answer); addExampleWhereClauseElement(answer); addBaseColumnListElement(answer); addBlobColumnListElement(answer); addSelectByExampleWithBLOBsElement(answer); addSelectByExampleWithoutBLOBsElement(answer); addSelectByPrimaryKeyElement(answer); addDeleteByPrimaryKeyElement(answer); addDeleteByExampleElement(answer); addInsertElement(answer); addInsertSelectiveElement(answer); addCountByExampleElement(answer); addUpdateByExampleSelectiveElement(answer); addUpdateByExampleWithBLOBsElement(answer); addUpdateByExampleWithoutBLOBsElement(answer); addUpdateByPrimaryKeySelectiveElement(answer); addUpdateByPrimaryKeyWithBLOBsElement(answer); addUpdateByPrimaryKeyWithoutBLOBsElement(answer); ... }
查看和定制。