java mybatis分页查询语句_mybatis分页查询的实现(一)[通俗易懂]

java mybatis分页查询语句_mybatis分页查询的实现(一)[通俗易懂]一、总结了mybatis中五种不同实现分页查询的方法UserMapper.java接口文件publicinterfaceUserMapper{//分页查询publicListselectForPage1(intstartIndex,intpageSize);publicListselectForPage2(Mapmap);publicIntegerselectCount()…

大家好,又见面了,我是你们的朋友全栈君。

一、总结了mybatis中五种不同实现分页查询的方法

UserMapper.java接口文件

public interface UserMapper {

//分页查询

public List selectForPage1(int startIndex,int pageSize);

public List selectForPage2(Map map);

public Integer selectCount();

public List selectForPage3(PageBean pageBean);

//分页加模糊查询

public Integer selectCount2(String keywords);

public List selectForPage4(Map map);

}

工具类PageBean.java

public class PageBean {

private Integer currentPage;

private Integer startIndex;

private Integer pageSize=5;

private Integer totalCount;

private Integer totalPage;

public Integer getCurrentPage() {

return currentPage;

}

public void setCurrentPage(Integer currentPage) {

this.currentPage = currentPage;

this.startIndex=(this.currentPage-1)*this.pageSize;

}

public Integer getPageSize() {

return pageSize;

}

public void setPageSize(Integer pageSize) {

this.pageSize = pageSize;

}

public Integer getTotalCount() {

return totalCount;

}

public void setTotalCount(Integer totalCount) {

this.totalCount = totalCount;

//计算总页数

this.totalPage=(int)Math.ceil((this.totalCount*1.0/this.pageSize));

}

public Integer getTotalPage() {

return totalPage;

}

public void setTotalPage(Integer totalPage) {

this.totalPage = totalPage;

}

public Integer getStartIndex() {

return startIndex;

}

public void setStartIndex(Integer startIndex) {

this.startIndex = startIndex;

}

}

UserMapper.xml文件

其中查询5是模糊加分页查询语句

/p>

PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”

“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>

select * from user limit #{param1},#{param2}

select * from user limit #{startIndex},#{pageSize}

select * from user

select count(*) from user

select * from user limit #{startIndex},#{pageSize}

select * from user

where name like “%”#{keywords}”%” or address like “%”#{keywords}”%”

limit #{startIndex},#{pageSize}

select count(*) from user where name like “%”#{value}”%” or address like “%”#{value}”%”

测试test

其中方法6是模糊加分页查询测试

public class myTest {

SqlSession session = MyBatisUtils.openSession();

UserMapper userMapper = session.getMapper(UserMapper.class);

@Test

public void selectForPage1() {

int currentPage=1;

int pageSize=5;

List selectForPage = userMapper.selectForPage1((currentPage-1)*pageSize, pageSize);

for (User user : selectForPage) {

System.out.println(user);

}

MyBatisUtils.closeSession(session);

}

@Test

public void selectForPage2() {

int currentPage=1;

int pageSize=5;

Map map=new HashMap<>();

map.put(“startIndex”, (currentPage-1)*pageSize);

map.put(“pageSize”, pageSize);

List selectForPage = userMapper.selectForPage2(map);

for (User user : selectForPage) {

System.out.println(user);

}

MyBatisUtils.closeSession(session);

}

@Test

public void selectForPage3() {

int currentPage=1;

int pageSize=5;

/**

* 参数1:开始条 偏移量,下标

* 参数2:参数总条数

*/

RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize, pageSize);

//使用mybatis里面提供的api去写的

List list = session.selectList(“com.gx.mapper.UserMapper.selectAll”, null, rowBounds);

for (User user : list) {

System.out.println(user);

}

MyBatisUtils.closeSession(session);

}

@Test

public void selectForPage4() {

Integer count = userMapper.selectCount();

System.out.println(count);

int currentPage=1;

int pageSize=5;

Map map=new HashMap<>();

map.put(“startIndex”, (currentPage-1)*pageSize);

map.put(“pageSize”, pageSize);

List list = userMapper.selectForPage2(map);

for (User user : list) {

System.out.println(user);

}

System.out.println(“当前第”+currentPage+”页,共”+count+”条”);

MyBatisUtils.closeSession(session);

}

@Test

public void selectForPage5() {

PageBean bean = new PageBean();

bean.setCurrentPage(1);

bean.setPageSize(5);

//查询总条数

Integer count = userMapper.selectCount();

//放到pageBean

bean.setTotalCount(count);

List list = userMapper.selectForPage3(bean);

for (User user : list) {

System.out.println(user);

}

System.out.println(“当前第”+bean.getCurrentPage()+”页,共”+count+”条”);

MyBatisUtils.closeSession(session);

}

@Test

public void selectForPage6() {

String keywords=”云6″;

PageBean bean = new PageBean();

bean.setCurrentPage(1);

bean.setPageSize(5);

//查询总条数

Integer count = userMapper.selectCount2(keywords);

//放到pageBean

bean.setTotalCount(count);

Map map = new HashMap<>();

map.put(“startIndex”, bean.getStartIndex());

map.put(“pageSize”, bean.getPageSize());

map.put(“keywords”, keywords);

List list = userMapper.selectForPage4(map);

for (User user : list) {

System.out.println(user);

}

System.out.println(“当前第”+bean.getCurrentPage()+”页,共”+count+”条”);

MyBatisUtils.closeSession(session);

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137118.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 矩阵特征值和特征向量怎么求_矩阵的特征值例题详解

    矩阵特征值和特征向量怎么求_矩阵的特征值例题详解设A是n阶方阵,如果存在数m和非零n维列向量 x,使得Ax=mx成立,则称m是A的一个特征值(characteristicvalue)或本征值(eigenvalue)。非零

    2022年8月5日
    3
  • MATLAB读取nc文件_如何转换mp3文件格式

    MATLAB读取nc文件_如何转换mp3文件格式因为课题处理30年的降雨和蒸发的遥感资料(.NC格式),而想要在Arcgis中处理要求的是raster格式的,所以需要批量转化为tif文件,所以在此分享自己改编之后的代码,可以简洁明了的实现这个过程:版本:MATLAB_2018bclc;clear;%%批读取NC文件的准备工作datadir=’G:\Global_P_ET\MSWEP_V2.2\’;%指定批量数据所在的文件夹…

    2022年10月23日
    0
  • ireport结构体介绍[通俗易懂]

    ireport结构体介绍[通俗易懂]一个报表的结构大致是几个部分:title、pageHeader、columnHeader、detail、columnFooter、pageFooter、lastPageFooter、summary以及groupHeader、groupfooter。如下图:·Title:每个报表一般会有一个名字,比如×××订单,title就是搁置这个名称的最好地方了,当然你也可以根据需要搁置在合适的地方。Title只在第一页出现。·pageHeader:报表的一些公共要素,比如页码、创建时间、创建人等信息放置在这里是比

    2022年9月10日
    0
  • Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream

    Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStreamJavaIO流学习总结三:缓冲流转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54292148本文出自【赵彦军的博客】InputStream|__FilterInputStream|__BufferedInputStream首先抛出一个问题,有了InputStream为什么还要有Buffered

    2022年10月20日
    0
  • Eclipse使用新手教程

    Eclipse使用新手教程

    2021年11月24日
    56
  • 弗洛伊德(Floyd)算法求图的最短路径「建议收藏」

    弗洛伊德(Floyd)算法求图的最短路径「建议收藏」弗洛伊德基本思想弗洛伊德算法作为求最短路径的经典算法,其算法实现相比迪杰斯特拉等算法是非常优雅的,可读性和理解都非常好。基本思想:弗洛伊德算法定义了两个二维矩阵:矩阵D记录顶点间的最小路径例如D[0][3]=10,说明顶点0到3的最短路径为10;矩阵P记录顶点间最小路径中的中转点例如P[0][3]=1说明,0到3的最短路径轨迹为:

    2022年6月4日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号