MyBatis中如何实现一个分页功能呢?
创始人
2024-02-22 06:22:22

转自:

MyBatis中如何实现一个分页功能呢?

分页可以毫不夸张的说:“是所有应用程序都必须拥有的操作模式”,那么MyBatis中如何实现一个数据库的分页取数据的功能呢?
下文将一一道来,如下所示:

实现思路:通过UserInfoMapper.xml中定义相应的sql脚本,
实现数据分页操作


例: UserInfoMapper中定义相应的方法

/**
*
* @param userInfo
* @param currentPageNo 起始位置
* @param pageSize      页面容量
* @return
*/
public List selectUserInfo(@Param("userInfo") UserInfo userInfo, @Param("from") Integer currentPageNo,@Param("pageSize") Integer pageSize);

UserInfoMapper.xml 中增加 limit 关键字,
SQL 映射代码

测试类

public static void main(String[] args) throws IOException {// 读取配置文件mybatis-config.xmlInputStream config = Resources.getResourceAsStream("mybatis-config.xml");// 根据配置文件构建SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);// 通过SqlSessionFactory创建SqlSessionSqlSession ss = ssf.openSession();UserInfo userInfo = new UserInfo();userInfo.setNotes("喜喜");Integer pageSize = 3;Integer currentPageNo = 0;List userList = new ArrayList();userList = ss.getMapper(UserInfoMapper.class).selectUserInfo(userInfo, currentPageNo, pageSize);for (UserInfo t : userList) {System.out.println(t);}
}

运行结果---
DEBUG [main] - ==> Preparing: SELECT id,name,notes FROM UserInfo where notes like concat ('%',?,'%') ORDER BY id limit ?,?
DEBUG [main] - ==> Parameters: 喜喜(String), 0(Integer), 3(Integer)
.....

相关内容

热门资讯

中外对话丨中外专家警告:日本主...   中新网北京12月15日电 题:中外专家警告:日本主动调整军事战略,或走向穷兵黩武  作者 管娜 ...
夏某某(男,大专学历)隐瞒精神... 转自:扬子晚报2024年参军入伍后在安徽出现精神类障碍被退回,2025年隐瞒病史后入伍再被退兵……1...
告别纸上谈兵!AI 培训找哪个...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:雷达财经)“...
一图读懂vivo S50:田曦...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:快科技)快科...
监管部门出手整治不正当价格行为... 近日,国家市场监督管理总局研究起草了《汽车行业价格行为合规指南(征求意见稿)》(下称《指南》),并向...