Mybatis+Oracle 分頁
1凹耙,使用mybatis的插件PageHelper
(1)Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:填寫的是監(jiān)聽的Mapper權限命名的接口 -->
<mapper namespace="cn.ssm.dao.StudentMapper">
<select id="selectUserinfo" resultType="cn.ssm.dao.bean.Userinfo">
select * from userinfo
</select>
</mapper>
復制
(2)分頁實現(xiàn)
public List<Userinfo> selectUserinfo() {
PageHelper.startPage(2, 3); //使用分頁插件
List<Userinfo> userinfos =studentMapper.selectUserinfo();
PageInfo<Userinfo> infos=new PageInfo<Userinfo>(userinfos);
// 這里獲得的list可能會多一個字段導致接口響應返回會有臟字段是尖,所以一般建議debug一下處理下陈轿。
List<Userinfo> list = infos.getList();
return userinfos;
}
復制
2溪食,使用利用 rownum這個字段嗜傅,oracle 數(shù)據(jù)庫中特有的字段
(1)Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:填寫的是監(jiān)聽的Mapper權限命名的接口 -->
<mapper namespace="cn.ssm.dao.StudentMapper">
<select id="selectUserinfo" parameterType="cn.ssm.dao.bean.Userinfo" resultType="cn.ssm.dao.bean.Userinfo">
select * from ( select row_.*, rownum rownum_ from ( select * from userinfo ) row_
where rownum <= #{pageNum} * #{pageSize} ) where rownum_ > (#{pageNum} - 1) * #{pageSize}
</select>
</mapper>
復制
(2)分頁實現(xiàn)
public class Userinfo {
private int id;
private String name;
private String password;
private Integer pageNum;
private Integer pageSize;
}
復制
public List<Userinfo> selectUserinfo() {
Userinfo user=new Userinfo();
user.setPageNum(2);//設置當前為第2頁
user.setPageSize(3);//設置每頁總共3條數(shù)據(jù)
List<Userinfo> userinfos = studentMapper.selectUserinfo(user);
return userinfos;
}
復制
結果展示:
[圖片上傳失敗...(image-858a4d-1693468685174)]
Mybatis實現(xiàn)Oracle 分頁「建議收藏」
例子:select * from (select t.,rownum r from (select * from A) t where rownum < 10)where r >5 第一層:select * from A:要查詢的數(shù)據(jù) 第二層:select t.,rownum r from (select * from A) t where rownum < 10:取前 10 行 第三層:select * from (select t.*,rownum r from (select * from A) t where rownum < 10)where r >5:取 5-10 行
說明: select * from ( select row_., rownum rownum_ from ( select * from userinfo ) row_ where rownum <= #{pageNum} * #{pageSize} )a where rownum_ > ( #{pageNum}- 1) * #{pageSize} 解析:先查詢select * from userinfo 總條數(shù)并起個別名 row_, 然后查詢select row_., rownum rownum_ from ( select * from userinfo ) row_ where rownum <= #{pageNum} * #{pageSize} 瘦锹,把所有數(shù)據(jù)和rownum固定字段列查詢并起別名畦戒, 最后從a中查詢出想要的數(shù)據(jù),主要是使用rownum固定字段列篩選實現(xiàn)分頁 where rownum_ > ( #{pageNum}- 1) * #{pageSize}