Skip to content

Commit 1c43318

Browse files
committed
update
1 parent 4899da5 commit 1c43318

File tree

19 files changed

+308
-15
lines changed

19 files changed

+308
-15
lines changed

.idea/libraries/lib_.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/lib___2_.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

book/book.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@
4141
<orderEntry type="library" name="mysql-connector-java-8.0.27-bin" level="project" />
4242
<orderEntry type="library" name="druid-1.1.21" level="project" />
4343
<orderEntry type="library" name="lib" level="project" />
44+
<orderEntry type="library" name="lib)" level="project" />
45+
<orderEntry type="library" name="lib (2)" level="project" />
4446
</component>
4547
</module>

book/src/main/java/com/atguigu/dao/BookDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ public interface BookDao {
1616

1717
public List<Book> queryBooks();
1818

19+
public Integer queryForPageTotalCount();
20+
21+
public List<Book> queryForPageItems(int begin, int pageSize);
1922
}

book/src/main/java/com/atguigu/dao/impl/BaseDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public Object queryForSingleValue(String sql,Object...args){
113113
return queryRunner.query(connection,sql,new ScalarHandler(),args);
114114
} catch (Exception e) {
115115
e.printStackTrace();
116+
}finally {
117+
JdbcUtils.close(connection);
116118
}
117119
return null;
118120
}

book/src/main/java/com/atguigu/dao/impl/BookDaoImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@ public List<Book> queryBooks() {
4040
String sql="select id ,`name`,author,price,sales,stock,img_path as imgPath from t_book ;";
4141
return queryForList(Book.class,sql);
4242
}
43+
44+
@Override
45+
public Integer queryForPageTotalCount() {
46+
String sql="select count(*) from t_book ;";
47+
Number count= (Number) queryForSingleValue(sql);
48+
return count.intValue();
49+
}
50+
51+
@Override
52+
public List<Book> queryForPageItems(int begin, int pageSize) {
53+
String sql="select id ,`name`,author,price,sales,stock,img_path as imgPath from t_book limit ?,?;";
54+
return queryForList(Book.class,sql,begin,pageSize);
55+
}
4356
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.atguigu.pojo;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Page是分页对象
7+
*
8+
* @param <T> 是具体的 模块的 javaBean类 , 可以适用于 不同类型对象的 分页显示功能
9+
*/
10+
public class Page<T> {
11+
12+
13+
14+
15+
public static final Integer PAGE_SIZE=2;//当前页数量(容量
16+
17+
18+
private Integer pageNo;//当前页码
19+
private Integer pageTotal;//总页码
20+
private Integer pageSize=PAGE_SIZE;//当前页数量(容量
21+
private Integer pageTotalCount;//总记录数
22+
private List<T> items;//当前页数据
23+
24+
public Integer getPageNo() {
25+
return pageNo;
26+
}
27+
28+
public void setPageNo(Integer pageNo) {
29+
//对pageNo 进行 限制范围
30+
if(pageNo<1){
31+
pageNo=1;
32+
}
33+
if(pageNo>pageTotal){
34+
pageNo=pageTotal;
35+
}
36+
this.pageNo = pageNo;
37+
}
38+
39+
public Integer getPageTotal() {
40+
return pageTotal;
41+
}
42+
43+
public void setPageTotal(Integer pageTotal) {
44+
this.pageTotal = pageTotal;
45+
}
46+
47+
public Integer getPageSize() {
48+
return pageSize;
49+
}
50+
51+
public void setPageSize(Integer pageSize) {
52+
this.pageSize = pageSize;
53+
}
54+
55+
public Integer getPageTotalCount() {
56+
return pageTotalCount;
57+
}
58+
59+
public void setPageTotalCount(Integer pageTotalCount) {
60+
this.pageTotalCount = pageTotalCount;
61+
}
62+
63+
public List<T> getItems() {
64+
return items;
65+
}
66+
67+
public void setItems(List<T> items) {
68+
this.items = items;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "Page{" +
74+
"pageNo=" + pageNo +
75+
", pageTotal=" + pageTotal +
76+
", pageSize=" + pageSize +
77+
", pageTotalCount=" + pageTotalCount +
78+
", items=" + items +
79+
'}';
80+
}
81+
}

book/src/main/java/com/atguigu/service/BookService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.atguigu.service;
22

33
import com.atguigu.pojo.Book;
4+
import com.atguigu.pojo.Page;
45

56
import java.util.List;
67

@@ -16,4 +17,7 @@ public interface BookService {
1617

1718
public List<Book> queryBooks();
1819

20+
21+
public Page<Book> page(int pageNo, int pageSize);
22+
1923
}

book/src/main/java/com/atguigu/service/impl/BookServiceImpl.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import com.atguigu.dao.BookDao;
44
import com.atguigu.dao.impl.BookDaoImpl;
55
import com.atguigu.pojo.Book;
6+
import com.atguigu.pojo.Page;
67
import com.atguigu.service.BookService;
78

89
import java.util.List;
910

10-
public class BookServiceImpl implements BookService {
11+
public class BookServiceImpl implements BookService {
1112

1213
// 接口的引用 = 实现了该接口的类的对象
1314
// 为了调用 BookDao的方法,必须获取到BookDaoImpl实现类的 对象
14-
private BookDao bookDao=new BookDaoImpl();
15+
private BookDao bookDao = new BookDaoImpl();
1516

1617

1718
@Override
@@ -38,4 +39,43 @@ public Book queryBookById(Integer id) {
3839
public List<Book> queryBooks() {
3940
return bookDao.queryBooks();
4041
}
42+
43+
@Override
44+
public Page<Book> page(int pageNo, int pageSize) {
45+
Page<Book> page = new Page<Book>();
46+
47+
48+
//设置每页数量
49+
page.setPageSize(pageSize);
50+
51+
//求总记录数
52+
Integer pageTotalCount = bookDao.queryForPageTotalCount();
53+
54+
//设置总记录数
55+
page.setPageTotalCount(pageTotalCount);
56+
57+
//求总页码
58+
Integer pageTotal = pageTotalCount / pageSize;
59+
if (pageTotalCount % pageSize > 0) {
60+
pageTotal += 1;
61+
}
62+
63+
//设置总页码
64+
page.setPageTotal(pageTotal);
65+
66+
67+
//设置当前页码
68+
page.setPageNo(pageNo);// 这个set方法 因为放了 限制范围的if语句 , 所以必须放在 设置总页码之前
69+
70+
71+
//求当前页的 起始索引
72+
int begin = (page.getPageNo() - 1) * pageSize;
73+
//求当前页数据
74+
List<Book> items = bookDao.queryForPageItems(begin,pageSize);
75+
//设置当前页数据
76+
page.setItems(items);
77+
78+
79+
return page;
80+
}
4181
}

book/src/main/java/com/atguigu/test/BookDaoTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77

88
import java.math.BigDecimal;
9+
import java.util.List;
910

1011
import static org.junit.Assert.*;
1112

@@ -42,4 +43,16 @@ public void queryBooks() {
4243
System.out.println(queryBook);
4344
}
4445
}
46+
47+
@Test
48+
public void queryForPageTotalCount() {
49+
System.out.println(bookDao.queryForPageTotalCount());
50+
}
51+
52+
@Test
53+
public void queryForPageItems() {
54+
for (Book pageItem : bookDao.queryForPageItems(8, 4)) {
55+
System.out.println(pageItem);
56+
}
57+
}
4558
}

0 commit comments

Comments
 (0)