大家好,又见面了,我是你们的朋友全栈君。
import java.util.List;
public class PageModel<E> {
//结果集
private List<E> list;
//查询记录数
private int totalRecords;
//每页多少条数据
private int pageSize;
//第几页
private int pageNo;
/**
* 总页数
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo() {
if (pageNo <= 1) {
return 1;
}
return pageNo - 1;
}
/**
* 下一页
* @return
*/
public int getNextPageNo() {
if (pageNo >= getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo() {
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
public class Demo {
private int id;
private String name;
public Demo(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
*
* @param pageNo 当前第几页
* @param pageSize 每页显示数据量
* @return
* @throws SQLException
*/
public PageModel getDate(int pageNo, int pageSize) throws SQLException {
String sql = "select * from demo limit ?,?";
PageModel pageModel = null;
PreparedStatement pstm = null;
ResultSet rs = null;
Demo demo = null;
Connection connection = null;
List<Demo> list = new ArrayList<>();
try {
//分页查询的数据
connection = getDataSource().getConnection();
pstm = connection.prepareStatement(sql);
pstm.setInt(1, (pageNo - 1) * pageSize);
pstm.setInt(2, pageSize);
rs = pstm.executeQuery();
while (rs.next()) {
list.add(new Demo(rs.getInt("id"),rs.getString("name")));
}
//查询总条数
ResultSet resultSet1 = pstm.executeQuery("select count(*) from demo");
int total = 0;
if (resultSet1.next()) {
total = resultSet1.getInt(1);
}
resultSet1.close();
//将数据放到分页工具类中
pageModel = new PageModel<>();
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(total);
pageModel.setList(list);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (connection != null) {
connection.close();
}
}
if (connection==null){
System.out.println("close");
}
if (connection!=null){
System.out.println(connection);
}
return pageModel;
}
//注解注入
Autowired
private JdbcService jdbcService;
@GetMapping("/getDemo")
public PageModel getDemo(int pageNo, int pageSize){
PageModel date = null;
try {
date = jdbcService.getDate(pageNo, pageSize);
} catch (SQLException e) {
e.printStackTrace();
}
return date;
}
参考:https://www.open-open.com/lib/view/open1346772322162.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/186465.html原文链接:https://javaforall.cn