Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

Nemo

关注TA

路漫漫其修远兮,吾将上下而求索。

  •  普罗旺斯
  • 负责帅就完事了
  • 写了1,496,113字

该文章投稿至Nemo社区   Java  板块 复制链接


Mybatis generator 分页插件

发布于 2018/03/27 16:38 2,270浏览 0回复 3,899


插件代码:


import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
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 java.util.List;

/**
* 分页插件
* @author: Nemo
* @date: 2018/3/27.
*/
public class MySQLPaginationPlugin extends PluginAdapter{

@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// add field, getter, setter for limit clause
addLimit(topLevelClass, introspectedTable, "startIndex");
addLimit(topLevelClass, introspectedTable, "pageSize");
addLimitMethod(topLevelClass, introspectedTable);
return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
}

@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
//$NON-NLS-1$
XmlElement isNotNullElement = new XmlElement("if");
//$NON-NLS-1$ //$NON-NLS-2$
isNotNullElement.addAttribute(new Attribute("test", "startIndex >= 0"));
isNotNullElement.addElement(new TextElement("limit ${startIndex} , ${pageSize}"));
element.addElement(isNotNullElement);
return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
}

private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
CommentGenerator commentGenerator = context.getCommentGenerator();
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(FullyQualifiedJavaType.getIntInstance());
field.setName(name);
field.setInitializationString("-1");
commentGenerator.addFieldComment(field, introspectedTable);
topLevelClass.addField(field);
char c = name.charAt(0);
String camel = Character.toUpperCase(c) + name.substring(1);
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("set" + camel);
method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name));
method.addBodyLine("this." + name + "=" + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
method.setName("get" + camel);
method.addBodyLine("return " + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
}

private void addLimitMethod(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
CommentGenerator commentGenerator = context.getCommentGenerator();
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("limit");
method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "startIndex"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "pageSize"));
method.addBodyLine("this.startIndex = startIndex;");
method.addBodyLine("this.pageSize = pageSize;");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
}

/**
* This plugin is always valid - no properties are required
*/
@Override
public boolean validate(List<String> warnings) {
return true;
}

}


使用:

直接在generator.xml文件中添加插件引用即可:

<plugin type="org.mybatis.generator.plugins.MySQLPaginationPlugin" /> <!-- 分页插件 -->
本文标签
 {{tag}}
点了个评