通用分页

分页三要素 PageBean类

建立一个PageBean类(分页工具类)
page 页码 视图层传递过来
rows 页大小 视图层传递过来
total 总记录数 后台查出来
pagination 是否分页 视图层传递过来

/**
 * 分页工具类
 *
 */
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;
}

实体类

public class Book {
	private int bid;
	private String bname;
	private float price;

	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Book(int bid, String bname, float price) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}
	public Book() {
		super();
	}

封装BaseDao

public class BaseDao<T> {
	
/**
 * @param sql      决定查询哪张表的数据
 * @param clz      查询出来的数据封装到哪个实体类中
 * @param pageBean 决定是否分页
 * @return  list   集合
 * @throws SQLException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{
	List<T> list = new ArrayList<>();
	java.sql.Connection con = null;
	java.sql.PreparedStatement ps = null;
	ResultSet rs = null;
	try {
		con = DBAccess.getConnection();
		
		if(pageBean !=null && pageBean.isPagination()) {
			//分页
			String countSql = getCountSql(sql);
			ps = con.prepareStatement(countSql);
			rs = ps.executeQuery();
			if(rs.next()) {
				pageBean.setTotal(rs.getLong(1)+"");
			}
			
			String pageSql = getPageSql(sql,pageBean);
			ps = con.prepareStatement(pageSql);
			rs = ps.executeQuery();
			
		}else {
			//不分页
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
		}
		while(rs.next()) {
			/**
			 * 1、创建一个Student对象
			 * 2、从 ResultSet 结果集中获取值给对象赋值
			 * 		2.1 获取到Student的属性对象
			 * 		2.2 给属性对象赋值
			 * 3、将已经有值的对象加入集合中
			 */
			
			//1、创建一个Student对象
			T t = (T) clz.newInstance();
			
			//2、从 ResultSet 结果集中获取值给对象赋值
			Field[] fs = clz.getDeclaredFields();
			for (Field f : fs) {
				f.setAccessible(true);
				f.set(t, rs.getObject(f.getName()));
			}
			
			//3、将已经有值的对象加入集合中
			list.add(t);
		}
	} finally {
		// shift+alt+z
		DBAccess.close(con);
	}
	
	return (List<T>) list;
}

/**
 * 将原生sql拼接出符合条件的某一页的数据查询sql
 * @param sql      原生sql
 * @param pageBean
 * @return
 */
private String getPageSql(String sql, PageBean pageBean) {
	return sql + "limit "+pageBean.getStartIndex()+","+pageBean.getRows();
}

/**
 * 用原生sql拼接出符合条件的总行数
 * @param sql  原生sql
 * @return
 */
private String getCountSql(String sql) {
	

return "select count(1) from ("+sql+") t";
	}
	
	
}

EncodingFiter类处理中文乱码代码如下:

/**
 * 中文乱码处理
 * 
 */
public class EncodingFiter implements Filter {

	private String encoding = "UTF-8";// 默认字符集

	public EncodingFiter() {
		super();
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		// 中文处理必须放到 chain.doFilter(request, response)方法前面
		res.setContentType("text/html;charset=" + this.encoding);
		if (req.getMethod().equalsIgnoreCase("post")) {
			req.setCharacterEncoding(this.encoding);
		} else {
			Map map = req.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合
			Set set = map.keySet();// 取出所有参数名
			Iterator it = set.iterator();
			while (it.hasNext()) {
				String name = (String) it.next();
				String[] values = (String[]) map.get(name);// 取出参数值[注:参数值为一个数组]
				for (int i = 0; i < values.length; i++) {
					values[i] = new String(values[i].getBytes("ISO-8859-1"),
							this.encoding);
				}
			}
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集
		if (null != s && !s.trim().equals("")) {
			this.encoding = s.trim();
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值