当我使用插件来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