MyBatis Generator 源码解读

Posted 小严

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis Generator 源码解读相关的知识,希望对你有一定的参考价值。

工欲善其事,必先利其器

在使用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);
...
}

查看和定制。

以上是关于MyBatis Generator 源码解读的主要内容,如果未能解决你的问题,请参考以下文章

mybatis Generator

mybatis-generator

13.1 MyBatis Generator概述(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》

mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)

mybatis源码学习利用maven插件自动生成mybatis代码

MyBatis源码解读——MapperMethod