mybatis IDEA——自定义分页和分页插件的使用_mybatis怎么设置自定义的分页的地方 ?-程序员宅基地

技术标签: IDEA  mybatis  

PageHelper实现了通用的分页查询,其支持的数据有,mysql、Oracle、DB2、PostgreSQL等主流的数据库。

使用说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/en/HowToUse.md

导入依赖

      <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.1.4</version>
      </dependency>

mybatis-config.xml中配置
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor" />
    </plugins>

接口

    Long getTotalRowCount(String keywords);
    
    Page<User> getUsersByPage(int pageIndex, int pageSize);

    Page<User> getUsersByPage(String keywords, int pageIndex, int pageSize);

    PageInfo<User> getUsersByPage2(int pageIndex, int pageSize);

    PageInfo<User> getUsersByPage2(String keywords, int pageIndex, int pageSize);

xml

 <select id="getTotalRowCount" resultType="java.lang.Long">
      select count(*) count
      from t_user
      where 1 = 1
      <if test="keywords != null and keywords != ''">
          and UserName like concat('%',#{keywords},'%')
      </if>
  </select>

<select id="getUsersByPage" resultType="User">
  select Id,
  UserName,
  Password
  from t_user
  where 1 = 1
  <if test="keywords != null and keywords != ''">
    and UserName like concat('%',#{keywords},'%')
  </if>
  limit #{startIndex},#{pageSize}
</select>

<select id="getUsersByPage2" resultType="User">
      select Id,
      UserName,
      Password
      from t_user
      where 1 = 1
      <if test="keywords != null and keywords != ''">
          and UserName like concat('%',#{keywords},'%')
      </if>
</select>

实体类

package com.etc.entity;

import java.util.List;

public class Page<T> {
	/**
	 * 每页条数
	 */
	private int pageSize = 10;
	
	/**
	 * 第几页 
	 */
	private int pageIndex = 1;
	
	/**
	 * 总的记录数 
	 */
	private long totalRowCount;
	
	/**
	 * 数据 
	 */
	private List<T> data;
	
	/**
	 * 是否有上一页 
	 */
	private boolean hasPrevPage;
	
	
	/**
	 * 是否有下一页 
	 */
	private boolean hasNextPage;


	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}


	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}


	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	/**
	 * @return the totalRowCount
	 */
	public long getTotalRowCount() {
		return totalRowCount;
	}


	/**
	 * @param totalRowCount the totalRowCount to set
	 */
	public void setTotalRowCount(long totalRowCount) {
		this.totalRowCount = totalRowCount;
	}


	/**
	 * @return the data
	 */
	public List<T> getData() {
		return data;
	}


	/**
	 * @param data the data to set
	 */
	public void setData(List<T> data) {
		this.data = data;
	}


	/**
	 * @return the totalPageCount
	 */
	public long getTotalPageCount() {
		if(this.totalRowCount % this.pageSize == 0){
			return this.totalRowCount / this.pageSize;
		}else{
			return this.totalRowCount / this.pageSize + 1;
		}
	}


	/**
	 * @return the hasPrevPage
	 */
	public boolean getHasPrevPage() {
		//什么情况下有上一页,如果当前页>1
		return this.pageIndex > 1;
	}


	/**
	 * @return the hasNextPage
	 */
	public boolean getHasNextPage() {
		//如果当前页和总的页数一样,则没有下一页,其它情况都有下一页
		return this.pageIndex == this.getTotalPageCount() ? false : true;
	}
	
	public Page(){
		
	}

	/**
	 * 有参构造方法
	 * @param pageSize 每页的条数
	 * @param pageIndex 当前页码
	 * @param totalRowCount 总的记录数
	 * @param data 数据
	 */
	public Page(int pageSize, int pageIndex, long totalRowCount, List<T> data) {
		super();
		this.pageSize = pageSize;
		this.pageIndex = pageIndex;
		this.totalRowCount = totalRowCount;
		this.data = data;
	}	
}

 servie实现类

  @Override
    public Long getTotalRowCount(String keywords){
        return userDao.getTotalRowCount(keywords);
    }

    @Override
    public Page<User> getUsersByPage(int pageIndex, int pageSize){
        int startIndex = (pageIndex - 1) * pageSize;
        List<User> users = userDao.getUsersByPage(null,startIndex,pageSize);
        Long totalRowCount = userDao.getTotalRowCount(null);
        com.etc.entity.Page<User> page = new com.etc.entity.Page<User>(pageIndex,pageSize,totalRowCount,users);
        return page;
    }

    @Override
    public  Page<User> getUsersByPage(String keywords, int pageIndex, int pageSize){
        int startIndex = (pageIndex - 1) * pageSize;
        List<User> users = userDao.getUsersByPage(keywords,startIndex,pageSize);
        Long totalRowCount = userDao.getTotalRowCount(keywords);
        com.etc.entity.Page<User> page = new com.etc.entity.Page<User>(pageIndex,pageSize,totalRowCount,users);
        return page;
    }

    @Override
    public  PageInfo<User> getUsersByPage2(int pageIndex, int pageSize){
        PageHelper.startPage(pageIndex,pageSize,true);
        List<User> users = userDao.getUsersByPage2(null);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public  PageInfo<User> getUsersByPage2(String keywords, int pageIndex, int pageSize){
        PageHelper.startPage(pageIndex,pageSize,true);
        List<User> users = userDao.getUsersByPage2(keywords);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

 测试类

 @Test
    /**
     * 自定义分页类,没有传递关键词参数
     */
    public void testGetUsersByPageWithoutKeywords(){
         com.etc.entity.Page<User> page = userService.getUsersByPage(3,2);
         System.out.println(page);
    }

    @Test
    /**
     * 自定义分页类,有传递关键词参数
     */
    public void testGetUsersByPageWithKeywords(){
        com.etc.entity.Page<User> page = userService.getUsersByPage("e",2,1);
        System.out.println(page);
    }


    @Test
    /**
     * 使用分页插件,没有传递关键词参数
     */
    public void testGetUsersByPage2WithoutKeywords(){
        com.github.pagehelper.PageInfo<User> page = userService.getUsersByPage2(3,2);
        System.out.println(page);
    }

    @Test
    /**
     * 使用分页插件,传递关键词参数
     */
    public void testGetUsersByPage2WithKeywords(){
        com.github.pagehelper.PageInfo<User> page = userService.getUsersByPage2("e",2,1);
        System.out.println(page);
    }
}

自定义分页查询jsp页面

        当前是第${page.pageIndex}页,共${page.totalPageCount }页,总记录数为${page.totalRowCount }条。
      <hr>
      <form action="UserServlet?op=getusersbypage" method="post" id="pageForm">
      	<input type="hidden" name="pageIndex" id="pageIndex" />
      	<button onclick="javascript:findPage(1)">首页</button>
      	<c:if test="${page.hasPrevPage }">
      		<button onclick="javascript:findPage(${page.pageIndex-1})">上一页</button>
      	</c:if>
      	<c:forEach begin="1" end="${page.totalPageCount }" step="1" var="index">
      	    <button onclick="javascript:findPage(${index})">${index }</button>
        </c:forEach>
      	<c:if test="${page.hasNextPage }">
      		<button onclick="javascript:findPage(${page.pageIndex+1})">下一页</button>
      	</c:if>    
      	<button onclick="javascript:findPage(${page.totalPageCount})">尾页</button> 
      	<input type="text" id="toPage" /> <button onclick="jump(${page.totalPageCount})">跳转</button>   
      </form>
      <script>
      	function findPage(pageIndex){
      		var form = document.getElementById('pageForm');
      		var page = document.getElementById('pageIndex') ;
      		page.value = pageIndex;
      		form.submit();
      	}
      	
      	function jump(totalPageCount){
      		var pageIndex = document.getElementById('toPage').value;
      		if(pageIndex < 1){
      			findPage(1);
      		}else if(pageIndex > totalPageCount){
      			findPage(totalPageCount);
      		}else{
      			findPage(pageIndex);
      		}
      	}

分页插件页面

         当前是第${page.pageNum}页,共${page.pages }页,总记录数为${page.total }条。
      <hr>
      <form action="UserServlet?op=getusersbypage" method="post" id="pageForm">
      	<input type="hidden" name="pageIndex" id="pageIndex" />
      	<button onclick="javascript:findPage(1)">首页</button>
      	<c:if test="${page.hasPreviousPage }">
      		<button onclick="javascript:findPage(${page.pageNum-1})">上一页</button>
      	</c:if>
      	<c:forEach begin="1" end="${page.pages }" step="1" var="index">
      	    <button onclick="javascript:findPage(${index})">${index }</button>
        </c:forEach>
      	<c:if test="${page.hasNextPage }">
      		<button onclick="javascript:findPage(${page.pageNum+1})">下一页</button>
      	</c:if>    
      	<button onclick="javascript:findPage(${page.pages})">尾页</button>
      	<input type="text" id="toPage" /> <button onclick="jump(${page.pages})">跳转</button>
      </form>
      <script>
      	function findPage(pageIndex){
      		var form = document.getElementById('pageForm');
      		var page = document.getElementById('pageIndex') ;
      		page.value = pageIndex;
      		form.submit();
      	}
      	
      	function jump(totalPageCount){
      		var pageIndex = document.getElementById('toPage').value;
      		if(pageIndex < 1){
      			findPage(1);
      		}else if(pageIndex > totalPageCount){
      			findPage(totalPageCount);
      		}else{
      			findPage(pageIndex);
      		}
      	}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Milan__Kundera/article/details/82503198

智能推荐

2.3.1 浮点数的表示(21)_浮点数尾码的补码-程序员宅基地

文章浏览阅读296次。浮点数的表示阶码 + 尾数阶码:常用补码或者移码尾数:定点小数阶码的底通常是2阶码的真值反映了需要前移和后移了多少位浮点数尾数的规格化通过算术左移,实现左规通过算术右移,实现右规左规:阶码减1右规:阶码加1如果双符号位出现溢出,需要右规规格化浮点数的特点1,原码表示的尾数:正数:0.1XXX,最大值0.1111;最小值0/1002,补码表示尾数符号位和数值位的最高位一定不同如果是正数,01如果是负数,10..._浮点数尾码的补码

MSVS下ARPACK数学库的编译--------使用MinGW编译并利用MSVS转译-程序员宅基地

文章浏览阅读163次。这周项目需要编译ARPACK库,感谢该博主的博文中的具体指导:“Visual studio 2015 +Windows10 配置ARPACK,用ARPACK求解特征值特征向量”,https://blog.csdn.net/barcelona941017/article/details/79727784。 说明: 1. 这种方式编译得到的数学库中的..._arpack怎么编译

C/C++ 错误处理-程序员宅基地

文章浏览阅读558次。has incomplete type and cannot be defined在头文件中添加该类型所在的文件eg:aggregate 'std::stringstream oss' has incomplete type and cannot be ;在文件中添加 include <sstream> 来解决。转载于:https://www.cnblogs.com/yuns..._has incomplete type and cannot be defined

R语言与主成分分析_r语言主成分分析例题详解-程序员宅基地

文章浏览阅读6.4k次,点赞13次,收藏127次。主成分分析实例例 9.1 (中学生身体四项指标的主成分分析)在某中学随机抽取某个年纪30名学生,测量其身高(X1)、体重(X2)、胸围(X3)和坐高(X4),数据如表9.1所示。试对这30名中学生身体四项指标数据做主成分分析。解析:用数据框的形式输入数据。用princomp()作主成分分析,由前面的分析,选择相关矩阵做主成分分析更合理。因此,这里选择的参数为cor=TRUE。最后用..._r语言主成分分析例题详解

鸿蒙 harmonyOS 如何手动关闭退出APP_鸿蒙代码关闭应用-程序员宅基地

文章浏览阅读345次。鸿蒙 harmonyOS 如何手动关闭退出APP呢?_鸿蒙代码关闭应用

海康4200门禁导入人脸_新品上手丨海康威视人脸门禁考勤一体机使用体验-程序员宅基地

文章浏览阅读1w次。原标题:新品上手丨海康威视人脸门禁考勤一体机使用体验在这里,你能获知 △ DS-K1T8101系列人脸门禁考勤一体机前不久,海康威视经销渠道发布了DS-K1T8101系列人脸门禁考勤一体机,小编也是第一时间从产品经理手中“抢”到了一台,带给大家第一手的使用体验。那么这款搭载了采用高性能处理器,搭配海康威视深度学习算法的“刷脸神器”,识别速度到底快不快,使用体验又怎么样呢?赶紧跟着小编的步伐来一起了..._海康威视人脸考勤系统

随便推点

探究Android的多分辨率支持以及各种类型图标尺寸大小-程序员宅基地

文章浏览阅读72次。术语和概念屏幕尺寸屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如2.8寸,3.5寸)。简而言之,Android把所有的屏幕尺寸简化为三大类:大,正常,和小。程序可以针对这三种尺寸的屏幕提供三种不同的布局方案,然后系统会负责把你的布局方案以合适的方式渲染到对应的屏幕上,这个过程是不需要程序员用代码来干预的。屏幕长宽比屏幕的物理长度与物理宽度的比例。程序可以为制定长宽比的屏幕提供制定的素材,..._android系统常用界面设计分辨率有那几个

mac的截图在linux下打不开,mac版截图软件Snip详细使用教程及常见问题-程序员宅基地

文章浏览阅读89次。怎么通过邮件分享 Snip 截图?在 Snip 的【偏好设置】中绑定你的QQ邮箱帐号,截屏时选定区域后点击分享的图标 ,即可跳到写信页面进行邮件分享。怎么使用滚动截屏?如果你从 Mac App Store 下载安装 Snip,该版本不支持滚动截屏。请在官网重新下载安装。如果你从官网下载安装 Snip,请在 Snip 的【偏好设置】中勾选“启动滚动截屏”(Firefox 不支持滚动截屏)。如何同时对..._snip打开找不到

<python> sqlarchemy模块应用(示例)_python 金仓 sqlarchemy-程序员宅基地

文章浏览阅读161次。<一>文件名: alchemy_connetct.py #连接数据库并创建表from sqlalchemy import create_engine, Column, Integer, String, Date, ForeignKeyfrom sqlalchemy.ext.declarative import declarative_basefrom sq..._python 金仓 sqlarchemy

pl/sql之cursor_plsql cursor-程序员宅基地

文章浏览阅读678次。游标的类型:1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql。2,显式游标:显式游标用于处理返回多行的查询。3,REF 游标:REF 游标用于处理运行时才能确定的动态 SQL 查询的结果在PL/SQL中使用DML语句时自动创建隐式游标;隐式游标自动声明、打开和关闭,其名为 SQL;通过检查隐式游标的属性可以获得最近执行的DML 语句的信息;隐式游标的属性有:%FOUND :SQL 语句影响了一行或多行时为 TRUE;%NOTFOUND :_plsql cursor

CCKS 2018 | 最佳论文:南京大学提出 DSKG,将多层 RNN 用于知识图谱补全-程序员宅基地

文章浏览阅读4.2k次。本文转载自公众号:机器之心。 选自CCKS 2018作者:Lingbing Guo、Qingheng Zhang、Weiyi Ge、Wei Hu、Yuzh..._ccks2018数据集

vos3000无可用路由_vos无可用路由-程序员宅基地

文章浏览阅读920次。vos3000无可用路由_vos无可用路由