一個分頁對象比較簡單
package com.netmovie.ent.util;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class PaginationSupport {
7
8 private static final int DEFAULT_COUNT_ON_EACH_PAGE = 10;
9
10 private int startPage = 1;// 當前頁數
11
12 private int pageSize = DEFAULT_COUNT_ON_EACH_PAGE;// 每頁顯示條數
13
14 private int startIndex;// 分頁首行
15
16 private int pageCount = 0;// 頁數
17
18 private int totalCount;// 一共多少條記錄
19
20 private int[] indexes = new int[0];
21
22 private List items = new ArrayList();
23
24
25 public List getItems() {
26 return items;
27 }
28
29 public void setItems(List items) {
30 this.items = items;
31 }
32 public boolean isPages(){
33 if(this.pageCount<=1){
34 return false;
35 }else{
36 return true;
37 }
38 }
39 public boolean getIsNextPage() {
40 if(this.startPage>=this.getPageSize()){
41 return false;
42 }
43 return true;
44 }
45 public boolean getIsPreviousPage() {
46 if(this.startPage<=1){
47 return false;
48 }
49 return true;
50 }
51
52 public void setTotalCount(final int totalCount) {
53 if (totalCount > 0) {
54 this.totalCount = totalCount;
55 int count = totalCount / pageSize;
56 if (totalCount % pageSize > 0) {
57 count++;
58 }
59 indexes = new int[count];
60 for (int i = 0; i < count; i++) {
61 indexes[i] = pageSize * i;
62 }
63 this.pageCount = count;
64 } else {
65 this.pageCount = 0;
66 this.totalCount = 0;
67 }
68 }
69
70 public int getPreviousPage() {
71
72 if (getStartPage() >1) {
73 return getStartPage() - 1;
74 } else {
75 return 1;
76 }
77
78 }
79 public int getNextPage() {
80 if (getStartPage() <= pageCount) {
81 return getStartPage() + 1;
82 } else {
83 return pageCount;
84 }
85 }
86 public int getStartPage() {
87 return startPage;
88 }
89 public void setStartPage(int startPage) {
90 int _pageCount = getPageCount();
91 if (startPage <=1) {
92 this.startPage = 1;
93 } else if (startPage > _pageCount) {
94 this.startPage = _pageCount;
95 } else {
96 this.startPage = startPage;
97 }
98 setStartIndex(indexes[this.startPage - 1]);
99 }
100
101 public int getStartIndex() {
102 return startIndex;
103 }
104
105 public void setStartIndex(int startIndex) {
106 this.startIndex = startIndex;
107 }
108
109 public int getTotalCount() {
110 return totalCount;
111 }
112
113 public int getPageSize() {
114 return pageSize;
115 }
116
117 public void setPageSize(int pageSize) {
118 this.pageSize = pageSize;
119 }
120
121 public int getPageCount() {
122 return pageCount;
123 }
124
125 public void setPageCount(int pageCount) {
126 this.pageCount = pageCount;
127 }
128 }
129
130 dao
131 public static PaginationSupport newsByMovie(final int startPage,final int pageSize){
132 Connection con = null;
133 PreparedStatement ps = null;
134 ResultSet rs = null;
135 PaginationSupport pageSupport = new PaginationSupport();
136 List items = new ArrayList();
137 try {
138
139 con = DbHelp.getConnection();
140 String sqlCount = "select count(1) from ent_news_prop " +
141 "t left outer JOIN ent_news ent on(t.new_id=ent.nid) ";
142 ps = con.prepareStatement(sqlCount);
143 rs = ps.executeQuery();
144 int totalCount = 0;
145 while(rs.next()){
146 totalCount = rs.getInt(1);
147 }
148 pageSupport.setPageSize(pageSize);
149 pageSupport.setTotalCount(totalCount);
150 pageSupport.setStartPage(startPage);
151 StringBuffer sb = new StringBuffer();
152 sb.append("SELECT * FROM ( SELECT tt.*,ROWNUM r FROM ( ");
153 String sql = "select t.new_id as newsId,t.movie_id as movieId,ent.title as title,ent.createdate as pubDate,ent.url url from ent_news_prop " +
154 "t left outer JOIN ent_news ent on(t.new_id=ent.nid) ";
155 sb.append(sql);
156 sb.append(") tt WHERE ROWNUM <= ? ) where r>= ? ");
157 ps = con.prepareStatement(sb.toString());
158 ps.setLong(1,pageSupport.getStartIndex()+pageSupport.getPageSize()-1);
159 ps.setLong(2,pageSupport.getStartIndex());
160 rs = ps.executeQuery();
161 MovieNews movieNews = null;
162 Date pubDate = null;
163 int i = 0;
164
165 while (rs.next()) {
166 movieNews = new MovieNews();
167
168 movieNews.setNewsId(rs.getInt("newsId"));
169 movieNews.setMovieId(rs.getInt("movieId"));
170 movieNews.setTitle(rs.getString("title"));
171 pubDate = (Date)rs.getDate("pubDate");
172 movieNews.setPubDate(f.format(pubDate));
173 movieNews.setUrl(rs.getString("url"));
174 items.add(movieNews);
175 }
176 pageSupport.setItems(items);
177 }catch(SQLException e){
178 e.printStackTrace();
179 }finally {
180 DbHelp.close(rs, ps, con);
181 }
182 return pageSupport;
183 }
getPageCount()總頁數
getStartPage()當前頁
getNextPage() 下一頁
getPreviousPage() 上一頁
isPages() 是否已經分頁
getIsNextPage() 是否有下一頁
getIsPreviousPage() 是否有上一頁
getTotalCount()共有多少條
getItems() 結果集
setTotalCount(otalCount) 設置總行數
isPages() 是否有分頁
191 getIsNextPage() 是否有下一頁
192 getIsPreviousPage() 是否有上一頁
193 getTotalCount()共有多少條
194 getItems() 結果集
195 setTotalCount(final int totalCount) 設置總行數> totalCount) 設置總行數
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class PaginationSupport {
7
8 private static final int DEFAULT_COUNT_ON_EACH_PAGE = 10;
9
10 private int startPage = 1;// 當前頁數
11
12 private int pageSize = DEFAULT_COUNT_ON_EACH_PAGE;// 每頁顯示條數
13
14 private int startIndex;// 分頁首行
15
16 private int pageCount = 0;// 頁數
17
18 private int totalCount;// 一共多少條記錄
19
20 private int[] indexes = new int[0];
21
22 private List items = new ArrayList();
23
24
25 public List getItems() {
26 return items;
27 }
28
29 public void setItems(List items) {
30 this.items = items;
31 }
32 public boolean isPages(){
33 if(this.pageCount<=1){
34 return false;
35 }else{
36 return true;
37 }
38 }
39 public boolean getIsNextPage() {
40 if(this.startPage>=this.getPageSize()){
41 return false;
42 }
43 return true;
44 }
45 public boolean getIsPreviousPage() {
46 if(this.startPage<=1){
47 return false;
48 }
49 return true;
50 }
51
52 public void setTotalCount(final int totalCount) {
53 if (totalCount > 0) {
54 this.totalCount = totalCount;
55 int count = totalCount / pageSize;
56 if (totalCount % pageSize > 0) {
57 count++;
58 }
59 indexes = new int[count];
60 for (int i = 0; i < count; i++) {
61 indexes[i] = pageSize * i;
62 }
63 this.pageCount = count;
64 } else {
65 this.pageCount = 0;
66 this.totalCount = 0;
67 }
68 }
69
70 public int getPreviousPage() {
71
72 if (getStartPage() >1) {
73 return getStartPage() - 1;
74 } else {
75 return 1;
76 }
77
78 }
79 public int getNextPage() {
80 if (getStartPage() <= pageCount) {
81 return getStartPage() + 1;
82 } else {
83 return pageCount;
84 }
85 }
86 public int getStartPage() {
87 return startPage;
88 }
89 public void setStartPage(int startPage) {
90 int _pageCount = getPageCount();
91 if (startPage <=1) {
92 this.startPage = 1;
93 } else if (startPage > _pageCount) {
94 this.startPage = _pageCount;
95 } else {
96 this.startPage = startPage;
97 }
98 setStartIndex(indexes[this.startPage - 1]);
99 }
100
101 public int getStartIndex() {
102 return startIndex;
103 }
104
105 public void setStartIndex(int startIndex) {
106 this.startIndex = startIndex;
107 }
108
109 public int getTotalCount() {
110 return totalCount;
111 }
112
113 public int getPageSize() {
114 return pageSize;
115 }
116
117 public void setPageSize(int pageSize) {
118 this.pageSize = pageSize;
119 }
120
121 public int getPageCount() {
122 return pageCount;
123 }
124
125 public void setPageCount(int pageCount) {
126 this.pageCount = pageCount;
127 }
128 }
129
130 dao
131 public static PaginationSupport newsByMovie(final int startPage,final int pageSize){
132 Connection con = null;
133 PreparedStatement ps = null;
134 ResultSet rs = null;
135 PaginationSupport pageSupport = new PaginationSupport();
136 List items = new ArrayList();
137 try {
138
139 con = DbHelp.getConnection();
140 String sqlCount = "select count(1) from ent_news_prop " +
141 "t left outer JOIN ent_news ent on(t.new_id=ent.nid) ";
142 ps = con.prepareStatement(sqlCount);
143 rs = ps.executeQuery();
144 int totalCount = 0;
145 while(rs.next()){
146 totalCount = rs.getInt(1);
147 }
148 pageSupport.setPageSize(pageSize);
149 pageSupport.setTotalCount(totalCount);
150 pageSupport.setStartPage(startPage);
151 StringBuffer sb = new StringBuffer();
152 sb.append("SELECT * FROM ( SELECT tt.*,ROWNUM r FROM ( ");
153 String sql = "select t.new_id as newsId,t.movie_id as movieId,ent.title as title,ent.createdate as pubDate,ent.url url from ent_news_prop " +
154 "t left outer JOIN ent_news ent on(t.new_id=ent.nid) ";
155 sb.append(sql);
156 sb.append(") tt WHERE ROWNUM <= ? ) where r>= ? ");
157 ps = con.prepareStatement(sb.toString());
158 ps.setLong(1,pageSupport.getStartIndex()+pageSupport.getPageSize()-1);
159 ps.setLong(2,pageSupport.getStartIndex());
160 rs = ps.executeQuery();
161 MovieNews movieNews = null;
162 Date pubDate = null;
163 int i = 0;
164
165 while (rs.next()) {
166 movieNews = new MovieNews();
167
168 movieNews.setNewsId(rs.getInt("newsId"));
169 movieNews.setMovieId(rs.getInt("movieId"));
170 movieNews.setTitle(rs.getString("title"));
171 pubDate = (Date)rs.getDate("pubDate");
172 movieNews.setPubDate(f.format(pubDate));
173 movieNews.setUrl(rs.getString("url"));
174 items.add(movieNews);
175 }
176 pageSupport.setItems(items);
177 }catch(SQLException e){
178 e.printStackTrace();
179 }finally {
180 DbHelp.close(rs, ps, con);
181 }
182 return pageSupport;
183 }
getPageCount()總頁數
getStartPage()當前頁
getNextPage() 下一頁
getPreviousPage() 上一頁
isPages() 是否已經分頁
getIsNextPage() 是否有下一頁
getIsPreviousPage() 是否有上一頁
getTotalCount()共有多少條
getItems() 結果集
setTotalCount(otalCount) 設置總行數
isPages() 是否有分頁
191 getIsNextPage() 是否有下一頁
192 getIsPreviousPage() 是否有上一頁
193 getTotalCount()共有多少條
194 getItems() 結果集
195 setTotalCount(final int totalCount) 設置總行數> totalCount) 設置總行數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.