mybatis generator dao扩展

当我使用插件来mybatis-paginator解决mybatis的分页问题时,我需要在mybatis generator插件生成的dao代码中加上下面的代码,这就涉及到mybatis generator源码的改造
PageList<AbmEmployee> selectByExample(AbmEmployeeExample example,PageBounds pageBounds);
分页插件的pom

<dependency>
	<groupId>com.github.miemiedev</groupId>
	<artifactId>mybatis-paginator</artifactId>
</dependency>

第一步
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType中添加

/**
     * 增加分页的list
     * @author dzm at 2016-10-20
     * @return
     */
    public static final FullyQualifiedJavaType getNewPageListInstance(){
    	return new FullyQualifiedJavaType("com.github.miemiedev.mybatis.paginator.domain.PageList");
    }

第二步
org.mybatis.generator.api.DAOMethodNameCalculator中添加

String getSelectByExampleWithoutBLOBsPagingMethodName(IntrospectedTable introspectedTable);

org.mybatis.generator.internal.DefaultDAOMethodNameCalculator中添加

/**
     * 分页查询
     * @author dzm
     */
    @Override
    public String getSelectByExampleWithoutBLOBsPagingMethodName(
    		IntrospectedTable introspectedTable) {
    	return "selectByExample";
    }

org.mybatis.generator.internal.ExtendedDAOMethodNameCalculator中添加

/**
     * @author dzm
     */
    @Override
    public String getSelectByExampleWithoutBLOBsPagingMethodName(
    		IntrospectedTable introspectedTable) {
    	return getSelectByExampleWithoutBLOBsMethodName(introspectedTable);
    }

第三步
org.mybatis.generator.internal.rules.Rules中添加生成规则

/**
	 * 产生带分页的selectByExample
	 * @return
	 */
	boolean generateSelectByExampleWithoutBLOBsPaging();

org.mybatis.generator.internal.rules.BaseRules中添加实现

@Override
    public boolean generateSelectByExampleWithoutBLOBsPaging() {
    	return true;
    }

org.mybatis.generator.internal.rules.RulesDelegate中添加

@Override
	public boolean generateSelectByExampleWithoutBLOBsPaging() {
		return rules.generateSelectByExampleWithoutBLOBsPaging();
	}

第四步
org.mybatis.generator.codegen.mybatis3.javamapper.elements中添加类

import static org.mybatis.generator.internal.util.messages.Messages.getString;

import java.util.Set;
import java.util.TreeSet;

import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;


/**
 * 分页的selectByExample
 * 示例如下:
 *   PageList<IcstFaq> selectByExample(IcstFaqExample example,PageBounds pageBounds);
 * @author dzm
 *
 */
public class SelectByExampleWithoutBLOBsMethodPagingGenerator extends AbstractJavaMapperMethodGenerator{
	
	public SelectByExampleWithoutBLOBsMethodPagingGenerator(){
		super();
	}


	@Override
    public void addInterfaceElements(Interface interfaze) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                introspectedTable.getExampleType());
        importedTypes.add(type);
        importedTypes.add(FullyQualifiedJavaType.getNewPageListInstance());

        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);

        FullyQualifiedJavaType returnType = FullyQualifiedJavaType
                .getNewPageListInstance();
        FullyQualifiedJavaType listType;
        if (introspectedTable.getRules().generateBaseRecordClass()) {
            listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        } else if (introspectedTable.getRules().generatePrimaryKeyClass()) {
            listType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
        } else {
            throw new RuntimeException(getString("RuntimeError.12")); //$NON-NLS-1$
        }

        importedTypes.add(listType);
        returnType.addTypeArgument(listType);
        method.setReturnType(returnType);

        method.setName(introspectedTable.getSelectByExampleStatementId());
        method.addParameter(new Parameter(type, "example")); //$NON-NLS-1$
        
        // 添加分页参数
        FullyQualifiedJavaType pagingType = new FullyQualifiedJavaType("com.github.miemiedev.mybatis.paginator.domain.PageBounds");
        importedTypes.add(pagingType);// import com.github.miemiedev.mybatis.paginator.domain.PageBounds
        method.addParameter(new Parameter(pagingType, "pageBounds")); 

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        addMapperAnnotations(interfaze, method);
        
        if (context.getPlugins()
                .clientSelectByExampleWithoutBLOBsMethodGenerated(method,
                        interfaze, introspectedTable)) {
            interfaze.addImportedTypes(importedTypes);
            interfaze.addMethod(method);
        }
    }

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

更改org.mybatis.generator.codegen.mybatis3.javamapper.JavaMapperGenerator中的实现
getCompilationUnits方法中添加

addSelectByExampleWithoutBLOBsPagingMethod(interfaze);

另外新增方法

/**
     * 添加带分页的selectByExample
     * @author dzm
     * @param interfaze
     */
    protected void addSelectByExampleWithoutBLOBsPagingMethod(Interface interfaze){
    	if (introspectedTable.getRules().generateSelectByExampleWithoutBLOBsPaging()){
    		AbstractJavaMapperMethodGenerator methodGenerator = new SelectByExampleWithoutBLOBsMethodPagingGenerator();
            initializeAndExecuteGenerator(methodGenerator, interfaze);
    	}
    }

插件源码位于mybatis_generator_extend

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值