MyBatis Generator实现MySQL分页插件,新增插件PaginationPlugin方法

项目链接:https://github.com/daydayRen/Mybaties-Generator

MyBatis Generator本身是有分页功能,废话不多说,让我们先看看他是怎么实现的:

我使用的Generator

MyBatis Generator实现MySQL分页插件,新增插件PaginationPlugin方法_第1张图片

配置文件generatorConfig.xm:





	
	
		
		
		
		
        
        
        
        
         
        
        
        
        
        
		
		 
		      
          
	        
	        
          
		 
          
	          
	        
          
		   
          
	          
	        
          
 
		
		
			
			
			
		
		
		
		
		
		

		
		
			
		


		
		
			
			
			
			
		

        
		
			
			
		
		
		
			
			
		
		

		
		

旧 RowBoundsPlugin:

generatorConfig.xml配置自带的分页功能

  
        

运行主函数生成

int offset = 100;
int limit = 25;
RowBounds rowBounds = new RowBounds(offset, limit);
List list = TMapper.selectByExampleWithRowbounds(example, rowBounds);

 RowBounds的构造方法new RowBounds(offset, limit)中的offset、limit参数就相当于MySQL的select语句limit后的offset和rows。如果此时仔细观察一下日志打出来的SQL语句或者看下生成的XxxMapper.xml文件中的selectByExampleWithRowbounds元素,可以发现select语句并没有使用limit。实际上RowBounds原理是通过ResultSet的游标来实现分页,也就是并不是用select语句的limit分页而是用Java代码分页,查询语句的结果集会包含符合查询条件的所有数据,使用不慎会导致性能问题,所以并不推荐使用RowBoundsPlugin来实现分页

新 PaginationPlugin:

实现过程:

首先创建我们的实现代码

package org.mybatis.generator.plugins.page;
import java.util.List;

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.Field;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
 
/**
 * 

File name : PaginationPlugin.java

*

Author : fly

*

Date : 2013-7-2 上午11:50:45

*

Extent:2018 yitianRen * 添加注释,方便理解和实现更多功能 */ public class PaginationPlugin extends PluginAdapter { @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { // add field, getter, setter for limit clause addPageNo(topLevelClass, introspectedTable, "pageNo");//当前页 addStartRow(topLevelClass, introspectedTable, "startRow");//开始行 addPageSize(topLevelClass, introspectedTable, "pageSize");//每页行数 return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } /** * 添加查询分页功能 */ @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated( XmlElement element, IntrospectedTable introspectedTable) { // XmlElement isParameterPresenteElemen = (XmlElement) element // .getElements().get(element.getElements().size() - 1); XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$ if标签 //test="startRow != null" sql语句 isNotNullElement.addAttribute(new Attribute("test", "startRow != null")); //$NON-NLS-1$ //$NON-NLS-2$ // isNotNullElement.addAttribute(new Attribute("compareValue", "0")); //$NON-NLS-1$ //$NON-NLS-2$ isNotNullElement.addElement(new TextElement("limit #{startRow} , #{pageSize}")); // isParameterPresenteElemen.addElement(isNotNullElement); element.addElement(isNotNullElement); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } private void addStartRow(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); // field.setType(FullyQualifiedJavaType.getIntInstance()); field.setType(PrimitiveTypeWrapper.getIntegerInstance()); 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(PrimitiveTypeWrapper.getIntegerInstance(), name)); method.addBodyLine("this." + name + "=" + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(PrimitiveTypeWrapper.getIntegerInstance()); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } private void addPageSize(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); // field.setType(FullyQualifiedJavaType.getIntInstance()); field.setType(PrimitiveTypeWrapper.getIntegerInstance()); field.setName(name); field.setInitializationString("10"); 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(PrimitiveTypeWrapper.getIntegerInstance(), name)); method.addBodyLine("this." + name + "=" + name + ";"); //this.startRow = (pageNo-1)*this.pageSize; method.addBodyLine("this.startRow = (pageNo-1)*this." + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(PrimitiveTypeWrapper.getIntegerInstance()); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } private void addPageNo(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); // field.setType(FullyQualifiedJavaType.getIntInstance()); field.setType(PrimitiveTypeWrapper.getIntegerInstance()); 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(PrimitiveTypeWrapper.getIntegerInstance(), name)); method.addBodyLine("this." + name + "=" + name + ";"); method.addBodyLine("this.startRow = (" + name + "-1)*this.pageSize;"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(PrimitiveTypeWrapper.getIntegerInstance()); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } /** * This plugin is always valid - no properties are required * 这个插件总是有效的——不需要属性 */ public boolean validate(List warnings) { return true; } }

 

配置:MyBatis Generator实现MySQL分页插件,新增插件PaginationPlugin方法_第2张图片

运行主函数:

你会看到我们的Example类中多了三个字段,就是我们最开始分页使用的一些参数

public class AinfoExample {
    protected String orderByClause;

    protected boolean distinct;

    protected List oredCriteria;

    protected String fields;

    protected Integer pageNo = 1;//当前页

    protected Integer startRow;//开始位置

    protected Integer pageSize = 10;//每页大小
......

Mapper.xml中:

MyBatis Generator实现MySQL分页插件,新增插件PaginationPlugin方法_第3张图片

使用limit分页完成

还有另一种实现方式感觉也不错,可能习惯了 以前的分页的方式还没有尝试,链接:https://blog.csdn.net/wo240/article/details/52576711

你可能感兴趣的:(java经验,Mybaties工具类)