mybatis-generator 添加分页操作

    源码:https://git.oschina.net/alexgaoyh/MutiModule-parent.git

    效果图:

    左边为分页操作的两个链接,默认一页10条数据,右边为数据库中的数据,页面没有处理,只是单纯的数据展现。


之前使用mybatis-generator插件生成了通用的代码部分,下面介绍一些修改点:

插件生成的 DemoExample 类,增加新的属性 (get set方法省略)

protected MyRowBounds myRowBounds;

插件生成的 DemoMapper.xml部分,新增

<sql id="myRowBoundsSQL">
  	<if test="myRowBounds != null">
  		limit ${myRowBounds.offset}, ${myRowBounds.limit}
  	</if>
  </sql>



并且将这一部分内容 添加到 
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.alexgaoyh.MutiModule.persist.demo.DemoExample" >

这个select标签的最后一部分,意味着如果 DemoExample  类中的 myRowBounds 属性如果不为空的话,在根据条件进行数据查询的时候,能够根据数据进行limit,分页操作。


其他地方不需要做任何修改,以上的两个地方的修改,就已经能够满足分页操作了。


下面附上两个util类

package com.alexgaoyh.MutiModule.persist.util;

import java.io.Serializable;

/**
 *  与  org.apache.ibatis.session.RowBounds 类似,添加了pageNumber属性
 *  
 * @author alexgaoyh
 *
 */
public class MyRowBounds implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2860692762946249134L;
	
	public final static int NO_ROW_OFFSET = 0;
	public final static int NO_ROW_LIMIT = Integer.MAX_VALUE;
	public final static MyRowBounds DEFAULT = new MyRowBounds();
	
	public final static int NO_PAGENUMBER = 0;

	//偏移量
	private int offset;
	//条数
	private int limit;
	//页码
	private int pageNumber;

	public MyRowBounds() {
		this.offset = NO_ROW_OFFSET;
		this.limit = NO_ROW_LIMIT;
		this.pageNumber = NO_PAGENUMBER;
	}

	public MyRowBounds(int offset, int limit, int pageNumber) {
	    this.offset = offset;
	    this.limit = limit;
	    this.pageNumber = pageNumber;
	}
	
	/**
	 * 返回RowBounds实体信息
	 * @param pageNumber 页码
	 * @param pageSize 一页条数
	 */
	public MyRowBounds(int pageNumber, int pageSize) {
		this.pageNumber = pageNumber;
		this.limit = pageSize;
		this.offset = (pageNumber - 1) * pageSize;
	}
	

	public int getOffset() {
		return offset;
	}

	public int getLimit() {
		return limit;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
}



package com.alexgaoyh.MutiModule.persist.util;

import java.io.Serializable;
import java.util.List;

/**
 * 分页类
 * @author alexgaoyh
 *
 * @param <E>
 */
public class Pagination<E> implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5631795318226681173L;

	private int total;
	
	private List<E> data;
	
	private MyRowBounds rowBounds;
	
	public Pagination(MyRowBounds rowBounds, int total) {
		
		this.rowBounds = new MyRowBounds(rowBounds.getOffset(), rowBounds.getLimit(), rowBounds.getPageNumber());
		
		this.total = total;
		
	}
	
	public Pagination(MyRowBounds rowBounds, int total, List<E> data) {
		
		this.rowBounds = new MyRowBounds(rowBounds.getOffset(), rowBounds.getLimit(), rowBounds.getPageNumber());
		
		this.total = total;
		
		this.data = data;
		
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<E> getData() {
		return data;
	}

	public void setData(List<E> data) {
		this.data = data;
		if(data != null && data.size() > this.total) {
			this.data = null;
		}
	}

	public MyRowBounds getRowBounds() {
		return rowBounds;
	}

	public void setRowBounds(MyRowBounds rowBounds) {
		this.rowBounds = new MyRowBounds(rowBounds.getOffset(), rowBounds.getLimit(), rowBounds.getPageNumber());
	}
	
	
	/**
	 * 自动以方法,获取页码总数
	 */
	public int getPageTotal() {
		int size = this.rowBounds.getLimit();
		return (int)Math.ceil( ((double)this.total * 1.0D) / (double)size );
	}
}



PS:

1:网络上有很多通用Mapper的开源项目,能够完成分页操作,简单看了一下,能够实现,但是大部分都使用了监听器,监听执行的sql,在进行处理,个人觉得这样操作的话,对sql都进行监听,在拼接sql,在某种程度上违背了mybatis的思想(sql 写在mapper.xml内部,sql与java类分开)。所以采用了上面的执行方法;

2:源码部分已经上传到git,有需要可以自行下载(源码迭代较快,可能后期相关部分测试代码会被屏蔽,但大体功能不会进行屏蔽,见谅);





转载于:https://my.oschina.net/alexgaoyh/blog/405489

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值