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)
上一篇 2022年6月2日 下午3:36
下一篇 2022年6月2日 下午3:46


相关推荐

  • java lamba表达式_非负简单函数表达式如何理解

    java lamba表达式_非负简单函数表达式如何理解在写之前,我在考虑是否要写这篇文章,然而当我查阅“lambda表达式”的相关内容的时候,我发现大量的文章都出现了冗余的现象,文章的篇幅过于夸张,严重影响了大家阅读的兴趣,因此我决定写一篇关于lambda表达式的文章,让大家能够在一定程度上对lambda表达式有一定的理解。希望能够对你在Java的学习中起到一定的帮助。

    2025年7月10日
    5
  • OpenClaw怎么配置?新手入门到进阶优化完整教程

    OpenClaw怎么配置?新手入门到进阶优化完整教程

    2026年3月16日
    2
  • android平台db4o使用示例

    android平台db4o使用示例db4o是一个纯对象的数据库,现支持java和.NET。07年的时候,db4objects宣布db4o已可以顺利运行于Android平台,当时db4objects公司和db4o的爱好者积极推进db4o运行于GoogleAndroid平台,以期待让db4o成为Android上的首选数据库平台,但令人遗憾的是google最后选择了sqlite作为作为android平台的数据库。但是没有关系,db4o

    2022年7月21日
    16
  • c# combobox属性dropdownlist赋值

    c# combobox属性dropdownlist赋值combobox属性设置为dropdownlist后,text赋值如果combobox的内容里有则显示,没有则不显示。 即如果combobox1包含ABCD,赋值A则可以显示,F则不行。

    2022年10月10日
    8
  • ffmeg将多段视频合成一个视频「建议收藏」

    ffmeg将多段视频合成一个视频「建议收藏」ffmeg将多段视频合成一个视频文章目录:一、方法一:利用文件列表二、方法二:不利用文件列表三.方法三:拼接不同编码格式的文件四、注意事项要处理多段视频太费劲啦,如果直接把多段小视频合成一段长视频处理起来就会方便很多,类似剪辑之后的视频合并操作!!!ffmpeg视频拼接需要用concat参数。此外ffmpeg拼接的视频和原视频的封装格式有关一、方法一:利用文件列表建立一个…

    2026年3月3日
    7
  • git添加用户名和邮箱「建议收藏」

    git添加用户名和邮箱「建议收藏」想了解更多数据结构以及算法题,可以关注微信公众号“数据结构和算法”,每天一题为你精彩解答。也可以扫描下面的二维码关注

    2025年8月28日
    6

发表回复

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

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