logo资料库

反射实现分页.doc

第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
资料共9页,剩余部分请下载后查看
反射封装分页数据查询
首先贴出分页算法中的实体类
再贴出分页的数据查询的封装
调用
解决乱码问题
反 射 封 装 分页数据查询 首先贴出分页算法中的实体类 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.manager.utils.GetConn; public class Page { // 当前页 private Integer nowPage; // 页面要显示信息条数 private Integer pageSize = 5; // 根据页面显示的条数计算总页数 private Integer countPage; // 根据传入的数据库查询数据库中的信息的条数 private Integer total; // 向数据库查询时的开始的下标 private Integer startIndex; // 向数据库查询时的查询条数 private Integer endIndex; // 页面页码个数 private Integer num = 5; // 页面页码总数 private String pageNum; //表名 private String tableName; /**
* 实体类(需带有封装查询条件) */ private T t; // 将查询到的数据存放到这里 private List rows = new ArrayList<>(); public Page(T t,String tableName) { setT(t); setTableName(tableName); } /** * 传入总记录数 和当前页 * * @param total * * @param nowPage * 当前页 */ public Page(T t,String tableName, Integer nowPage) { 总记录数 setT(t); setTableName(tableName); this.total = getDataTotal(tableName); // 赋值 this.nowPage = nowPage; // 判断当前页是否合法 if (this.nowPage < 1) { this.nowPage = 1; } // 计算总页数 this.countPage = this.total % this.pageSize == 0 ? this.total / this.pageSize : this.total / this.pageSize + 1; if (this.nowPage > this.countPage) { this.nowPage = this.countPage; } // 计算出开始的记录下标,和每页要显示的条数 if (this.nowPage == 0) { } else { this.startIndex = this.nowPage * this.pageSize; this.endIndex = this.pageSize; this.startIndex = (this.nowPage - 1) * this.endIndex = this.pageSize; this.pageSize;
} this.pageNum = getPageNums(); } /** * pageCount 总页数 pageCurr 当前页 num 显示的页码数 * * @return */ private String getPageNums() { StringBuffer sb = new StringBuffer(); if (num >= countPage) { sb.append(i).append(","); } else if (nowPage == countPage) { for (int i = 1; i <= countPage; i++) { } for (int i = countPage - num + 1; i <= countPage; break; if (i > countPage) { } sb.append(i).append(","); } else if (nowPage + num > countPage) { } for (int i = countPage - num; i < countPage; i++) break; if (i > countPage) { } sb.append(i).append(","); } else { } for (int i = nowPage; i < nowPage + num; i++) break; if (i > countPage) { } sb.append(i).append(","); i++) { { { } } return sb.toString(); } /** * 通过表名得到数据总条数 */ public int getDataTotal(String tableName) {
conn.prepareStatement(sql); int total = 0; Connection conn = GetConn.getConnection(); String sql = "select count(*) from "+tableName; try { total = set.getInt(1); PreparedStatement pd = ResultSet set = pd.executeQuery(); while (set.next()) { } set.close(); pd.close(); } catch (SQLException e) { e.printStackTrace(); } return total; } return tableName; this.tableName = tableName; public String getTableName() { } public void setTableName(String tableName) { } public T getT() { } public void setT(T t) { } public String getPageNum() { } public void setPageNum(String pageNum) { } public List getRows() { } this.pageNum = pageNum; return rows; return t; this.t = t; return pageNum;
this.rows = rows; return startIndex; return endIndex; return total; return nowPage; public void setRows(List rows) { } public Integer getStartIndex() { } public Integer getEndIndex() { } public Integer getTotal() { } public Integer getNowPage() { } public void setNowPage(Integer nowPage) { } public Integer getPageSize() { } public void setPageSize(Integer pageSize) { } public Integer getCountPage() { } public void setCountPage(Integer countPage) { } public void setTotal(Integer total) { } public void setStartIndex(Integer startIndex) { } this.startIndex = startIndex; this.countPage = countPage; this.pageSize = pageSize; this.nowPage = nowPage; return pageSize; return countPage; this.total = total;
public void setEndIndex(Integer endIndex) { } this.endIndex = endIndex; } 再贴出分页的数据查询的封装 /** * 动态查询语句封装 * * @param page * @param sql * @return */ public Page get(Page page) { Class cla = page.getT().getClass(); List list = new ArrayList<>(); T t = page.getT(); // 获取类中的定义的属性字段 Field[] fields = cla.getDeclaredFields(); Connection conn = GetConn.getConnection(); try { ").append(page.getTableName()).append(" where 1 = 1"); StringBuffer sqlBuff = new StringBuffer(); sqlBuff.append("select * from List lis = new ArrayList<>(); for (Field field : fields) { 名称的值 field.setAccessible(true); Object obj = field.get(t); // 得到对应字段 if(null != obj && !"".equals(obj)){ lis.add(obj); sqlBuff.append(" and ").append(field.getName()).append("=").append("?"); } } Integer startIndex = page.getStartIndex(); Integer endIndex = page.getEndIndex(); if(null != startIndex && null != endIndex){
"+page.getStartIndex()+","+page.getEndIndex()); sqlBuff.append(" limit conn.prepareStatement(sqlBuff.toString()); } PreparedStatement pd = for (int i = 0; i < lis.size(); i++) { pd.setObject(i+1,lis.get(i)); } ResultSet set = pd.executeQuery(); ResultSetMetaData rsmd = set.getMetaData(); while (set.next()) { @SuppressWarnings("unchecked") T clas = (T) cla.newInstance(); for (Field field : fields) { field.setAccessible(true); // 数据库中是从 1 开始的 for (int j = 0; j < String fieldName = if rsmd.getColumnCount(); j++) { rsmd.getColumnName(j + 1); (field.getName().equalsIgnoreCase(fieldName)) { field.getName().toCharArray(); Character.toUpperCase(chaArr[0]); String(chaArr); char[] chaArr = chaArr[0] = String methodName = new // 值的类型 Method fun = cla.getMethod("set" + methodName, new Class[] { field.getType() }); // 以 Obj 类型获得列中的值 Object val = fun.invoke(clas, new break; set.getObject(methodName); Object[] { val }); } } } list.add(clas); } set.close(); pd.close(); e.printStackTrace(); } catch (Exception e) {
} page.setRows(list); return page; } 调用 public Page getUpdate(String id){ ParkManager parkManager = new ParkManager(); //你查询要加什么条件就往这个对象中设什么值 parkManager.setId(id); //对象 表名 当前页 Page page = new return get(page); Page<>(parkManager ,"parkmanageuser",1); } 解决乱码问题 package com.manager.utils; import java.io.UnsupportedEncodingException; /** * 编码器 * */ public class EnCoder { private static final String code = "UTF-8"; /** * 返回指定编码格式 * * @param value * @param code * @return */
分享到:
收藏