当前位置:网站首页>通用分页01
通用分页01
2022-07-24 05:18:00 【zsm030616】
目录
通用分页的重点在于通用,今天我来给大家分享一下我对通用的理解,分页分前台和后台代码,今天带来的是后台代码
一、pageBean
pagebean:信息实体类,用来存放分页参数,将分页所需要的参数进行封装
封装的好处:方便使用,减少代码。
以下是分页所需要的参数
pagebean.javapackage com.zsm.util; /** * 分页工具类 * */ public class PageBean { private int page = 1;// 页码 private int rows = 10;// 页大小 private int total = 0;// 总记录数 private boolean pagination = true;// 是否分页 public PageBean() { super(); } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } /** * 获得起始记录的下标 * * @return */ public int getStartIndex() { return (this.page - 1) * this.rows; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } }
二、分页方法类
以书籍类为例子,我们在书籍Dao里继承BaseDao,我们只需要在返回时返回BaseDao中获取全部的方法 executeQuery,同时我们得到了三个参数sql,pagebean,以及CallBack,我们只需要将CallBack替换为书籍的结果集,再遍历书籍结果集就可以了
BookDao .java
package com.zsm.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zsm.entity.Book;
import com.zsm.util.DBAccess;
import com.zsm.util.PageBean;
import com.zsm.util.StringUtils;
public class BookDao extends BaseDao<Book>{
List<Book> list2(Book book,PageBean p) throws SQLException{
String sql = "select * from tb_book where 1=1";
String bname=book.getName();
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like '%"+bname+"%'";
}
int bid =book.getId();
if(bid!=0) {
sql +=" and bid="+bid;
}
return super.list(sql, p, rs ->{
List<Book> list = new ArrayList<>();
try {
while(rs.next()) {
list.add(new Book(rs.getInt("bid"), rs.getString("bname")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
});
}
public static void main(String[] args) throws Exception {
// List<Book> list = new BookDao().list(new Book(), null);
// for (Book book : list) {
// System.out.println(book);
// }
}
}
CallBack.java
package com.zsm.util;
import java.sql.ResultSet;
import java.util.List;
/**
* 回调函数接口类的作用
* 谁调用谁处理
* @author 周思敏
*
* @param <T>
*/
public interface CallBack<T> {
List<T> foreach(ResultSet rs);
}
BaseDao.java
package com.zsm.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zsm.entity.Book;
import com.zsm.util.CallBack;
import com.zsm.util.DBAccess;
import com.zsm.util.PageBean;
import com.zsm.util.StringUtils;
/**T代表的是实体类,可以使book/user/goods...
* @author 周思敏
*
* @param <T>
*/
public class BaseDao<T> {
List<T> list(String sql,PageBean p,CallBack<T> callback) throws SQLException{
//目的是为了得到总纪录数->总页数
/**
* 1、拿到数据库连接
* 2、拿到preparestatment
* 3、执行sql语句
*/
Connection con =null;//重复代码1
PreparedStatement ps = null;//重复代码2
ResultSet rs =null;//重复代码3
// 查询不同的表必然要处理不同的结果集
if(p !=null && p.isPagination()) {
//得到总记录数的SQL语句
String countsql=getCountSQL(sql);
con = DBAccess.getConnection();//重复代码1
ps = con.prepareStatement(countsql);//重复代码2
rs = ps.executeQuery();//重复代码3
if(rs.next()) {
//当前实体类就包含总纪录数
p.setTotal(rs.getString("n"));
}
//页数的SQL语句
String pageSQL= getpageSQL(sql,p);
con = DBAccess.getConnection();//重复代码1
ps = con.prepareStatement(pageSQL);//重复代码2
rs = ps.executeQuery();//重复代码3
System.out.println(pageSQL);
}
else{
con = DBAccess.getConnection();//重复代码1
ps = con.prepareStatement(sql);//重复代码2
rs = ps.executeQuery();//重复代码3
}
return callback.foreach(rs);
}
/**
* 拼装第n页的数据的sql
* @param sql
* @param p
* @return
*/
private String getpageSQL(String sql, PageBean p) {
// TODO Auto-generated method stub
return sql+" limit "+p.getStartIndex()+","+p.getRows();
}
/**
* 总记录数
* @param sql
* @return
*/
private String getCountSQL(String sql) {
// TODO Auto-generated method stub
return "select count(*) as n from ("+sql+") t";
}
}
public static void main(String[] args) throws Exception {
List<Book> list = new ArrayList<>();
Book b = new Book();
// b.setName("aa");//模糊查询就赋值
PageBean p = new PageBean();
//System.out.println(p);//设置第几页
list = new BookDao().list2(b, p);
for (Book book : list) {
System.out.println(book);
}
}
边栏推荐
- 还原ui设计稿
- 解决:控制台使用nvm控制node版本时出现exit status 1与exit status 145
- 7. 在屏幕上绘制一条贝塞尔曲线,并用反走样技术对曲线进行平滑处理。
- Constructor_ Map constructor
- JS - 计算直角三角形的边长及角度
- JS链表中的快慢指针
- Hurry in!! Take you to understand what is multi file, and easily master the usage of extern and static C language keywords!!!
- OpenGL simulates the process of a ball falling to the ground and bouncing up in real life. Draw a ball on the screen, and the ball will fall from top to bottom, hit the ground and bounce up again.
- Pure white tutorial using Druid database connection pool in idea
- Promise续(尝试自己实现一个promise)更详细的注释和其它接口暂未完成,下次咱们继续。
猜你喜欢
随机推荐
C2 random generation function seed, numpy. Random. Seed(), TF. Random. Set_ Seed Learning + reprint and sorting
C#进程运行权限
过渡 效果
C document reading and writing plus linked list addition, deletion, modification and query
赶紧进来!!轻松掌握C语言“顺序”、“分支”、“循环”三大结构
flex布局
Sorting out some common server instructions and some binding instructions in csgo
关于作为行业人从业人员的一点思考
还原ui设计稿
B站视频评论爬取——以鬼灭之刃为例(并将其存储到csv中)
String_ Method_ 01match method
Scikit learn notes
ros启动非本机节点
special effects - 鼠标移动,出现自定义的表情拖尾
3. 在屏幕上绘制一个底是正方形的五面锥体,锥体的底面在XOZ平面上,锥顶在Y轴上。用下图给锥体的四个三角形面做纹理映射,使得锥体的四个面分别是红橙黄绿色。
8.使用二次几何体技术,在屏幕上绘制一个上小下大的柱体。
[DP] number triangle
Modify jupyter save path
yocs_velocity_smoother源码编译
Geoserver自动化上传Shapefile









