mybatis+oracle实现分页查询--非常简单实用

来源:互联网 发布:照片字体识别软件 编辑:程序博客网 时间:2024/06/03 19:06

userInfo.xml

[java] view plain copy

1. </pre>前几天刚学习了mybatis,之后就在我写的某个列子中加上一层mybatis来访问oracle数据库,一般的增删改查经过测试都没有问题,唯一留下oracle的分页查询,一开始自然而然的就想当然的把oracle分页查询语句写进userInfo.xml中<p></p><p></p><pre name="code" class="html"><select id="queryUserListForPage" parameterclass="Page" resultmap="UserInfoResult">select * from (select t.*, ROWNUM RM from (select * from tb_user_info) t where ROWNUM<=#max# ) where RM> #offset#</select>  

 

但该xml文件会报语法错误,不信,运行果然出错了,尝试多种方法之后还是不行

 

1. <select id="queryUserListForPage" parameterClass="string" resultMap="UserInfoResult">  

2.         $sql$  

3.     </select>  

 

 

 

1. package com.is.dao.ibatis.impl;  

2.   

3. /** 

4.  * Oracle分页查询语句生成类 为ibatis实现分页设计 

5.  *  

6.  * @author qiulongjie 

7.  *  

8.  */  

9. public class PageInfo {  

10.     private int offset;  

11.     private int max;  

12.     private String sql;  

13.   

14.     public PageInfo() {  

15.     }  

16.   

17.     public PageInfo(int offset, int max) {  

18.         this.offset = offset;  

19.         this.max = max;  

20.         this.sql = "select * from (select t.*, ROWNUM RM from (select * from tb_user_info) t where ROWNUM<=" + this.max  

21.                 + " ) where RM> " + this.offset;  

22.         System.out.println(sql);  

23.     }  

24.   

25.     public String getSql() {  

26.         return sql;  

27.     }  

28.   

29.     public void setSql(String sql) {  

30.         this.sql = sql;  

31.     }  

32.   

33.     public int getOffset() {  

34.         return offset;  

35.     }  

36.   

37.     public void setOffset(int offset) {  

38.         this.offset = offset;  

39.     }  

40.   

41.     public int getMax() {  

42.         return max;  

43.     }  

44.   

45.     public void setMax(int max) {  

46.         this.max = max;  

47.     }  

48.   

49. } 

 

 

 

 

 

 

 

 

 

 

 

 

 

数据访问层的数据查询代码

 

1. public List<UserInfo> getUserInfoForPage(int offset, int max) {  

2.         List<UserInfo> userInfos = new ArrayList<UserInfo>();  

3.         try {  

4.             userInfos = IbatisUtil.getSqlMapClient().queryForList("queryUserListForPage",  

5.                     new PageInfo(offset, max).getSql());  

6.         } catch (SQLException e) {  

7.             e.printStackTrace();  

8.         }  

9.         return userInfos;  

10.     } 

 

原创粉丝点击