- 浏览: 3423 次
- 性别:
- 来自: 火星蛤蟆王国
最新评论
前几天做页面,发现有大量的分页查询页面要做,而且都是基于单表的,一开始想,用Criteria的example查询轻松啊,可是实现的时候遇到一个问题,花了一上午没搞定,再加之项目进度比较急,就想着用反射来取查询VO里得值来拼装HQL,忙乎了2个小时,做出个雏形暂时先用着,今天回头看到这个实现,感到还有一些地方可以改进,先把最初版本放上来,作为一个留念,也暂且作为我的JAVAEYE的懒人BLOG的开粪堆之做吧。
所有QueryVO继承BaseVO
DAO实现
HQLFittingShop实现
Pager实现
Page实现
页面上的处理
所有QueryVO继承BaseVO
public abstract class BaseVO { public static final String ASC = "asc"; public static final String DESC = "desc"; private int pageNo = 1; private String pageOrder = DESC; private boolean fuzzyQuery = true; private int resultsPerPage = Constants.PAGE_SIZE; private int pagePerGroup = Constants.PAGE_GROUP_SIZE; private String orderBy = "id"; public String getOrderBy() { return orderBy; } public void setOrderBy(String orderBy) { this.orderBy = orderBy; } ....
public class User extends BaseVO implements java.io.Serializable { /** * */ private static final long serialVersionUID = -8138345689576402414L; private Integer id; private String username; private String password; private String email; private String address; private String zip; private String mobilenum; private String tel; ......
DAO实现
public class BaseDAO extends HibernateDaoSupport { @SuppressWarnings( { "static-access", "unchecked" }) public Page findPageByEntityQuery(final BaseVO entity) throws Exception { final int pageNo = entity.getPageNo(); final int maxResults = entity.getResultsPerPage(); final int pagePerGroup = entity.getPagePerGroup(); HQLFittingShop hQLFittingShop = HQLFittingShop.getInstance(entity); final String _finalHql = hQLFittingShop.getHql(); final String _finalCountHql = hQLFittingShop.getCounthql(); final Object[] _finalValues = hQLFittingShop.getPram(); Page resultPage = (Page) getHibernateTemplate().execute( (new HibernateCallback() { public Object doInHibernate(Session session) { final String finalHql = _finalHql; final String finalCountHql = _finalCountHql; final Object[] finalValues = _finalValues; Query query = session.createQuery(finalHql); Query countQuery = session.createQuery(finalCountHql); for (int i = 0; i < finalValues.length; i++) { Object tempVal = finalValues[i]; query.setParameter(i, tempVal); countQuery.setParameter(i, tempVal); } int totalRecord = 0; totalRecord = ((Integer) countQuery.uniqueResult()) .intValue(); Pager pager = new QueryPager(query); return pager.getPage(maxResults, pageNo, pagePerGroup, totalRecord); } })); return resultPage; } } }
HQLFittingShop实现
public class HQLFittingShop { private final String hql; private final Object[] pram; private final String counthql; protected HQLFittingShop(final String finalHql, final Object[] finalPram, final String finalCountHql) { super(); this.hql = finalHql; this.pram = finalPram; this.counthql = finalCountHql; } @SuppressWarnings( { "static-access", "unchecked" }) public static HQLFittingShop getInstance(BaseVO entity) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, ClassNotFoundException { String pageOrder = entity.getPageOrder(); String orderBy = entity.getOrderBy(); boolean fuzzyQuery = entity.isFuzzyQuery(); String hql = "from " + entity.getClass().getName() + " a where 1=1 "; List valueList = new ArrayList(); PropertyUtils pu = new PropertyUtils(); PropertyDescriptor[] pd = pu.getPropertyDescriptors(entity.getClass()); BeanUtils bu = new BeanUtils(); PropertyUtils superpu = new PropertyUtils(); PropertyDescriptor[] superpd = superpu .getPropertyDescriptors(BaseVO.class); for (int i = 0; i < pd.length; i++) { if (bu.getProperty(entity, pd[i].getName()) != null) { boolean hasThisProprty = false; for (int j = 0; j < superpd.length; j++) { if (superpd[j].getName().equals(pd[i].getName())) { hasThisProprty = true; break; } } if (!hasThisProprty) { if (fuzzyQuery && "java.lang.String".equals(pd[i] .getPropertyType().getName())) { hql += "and a." + pd[i].getName() + " like ? "; valueList.add("%" + bu.getProperty(entity, pd[i].getName()) .trim() + "%"); } else { if (!("id".equals(pd[i].getName()) && "0".equals(bu .getProperty(entity, pd[i].getName())))) { hql += "and a." + pd[i].getName() + " = ? "; Object obj = ConstructorUtils.invokeConstructor( Class.forName(pd[i].getPropertyType() .getName()), bu.getProperty(entity, pd[i].getName())); valueList.add(obj); } } } } } hql += "order by a." + orderBy + " " + pageOrder; Object[] values = new Object[valueList.size()]; for (int j = 0; j < values.length; j++) { values[j] = valueList.get(j); } final String finalHql = hql; final Object[] finalPram = values; return new HQLFittingShop(finalHql, finalPram, "select count(a.id) " + hql); } public String getHql() { return hql; } public Object[] getPram() { return pram; } public String getCounthql() { return counthql; } }
Pager实现
public class QueryPager implements Pager { private final Query query; public QueryPager(final Query query) { this.query = query; } public Page getPage(final int maxResults, final int pageNumber, final int pagePerGroup,final int totalPageNumber) throws HibernateException { query.setFirstResult((pageNumber-1)*maxResults); query.setMaxResults(maxResults); List results = query.list(); Page pageImpl = new PageImpl(results, pageNumber, maxResults,pagePerGroup,totalPageNumber); return pageImpl; } }
Page实现
public class PageImpl implements Page { private final List results; private final int pageNumber; private final int groupNumber; private final int resultsPerPage; @Deprecated private final int pageGroupNumber; private final boolean last; private final boolean first; private final boolean lastGroup; private final boolean firstGroup; private final int pagePerGroup; private final int[] pageGroup; private final int lastGroupNumber; private final int firstGroupNumber = 1; private final int lastGroupPageNumber; private final int firstGroupPageNumber = 1; private final int lastPageNumber; private final int firstPageNumber = 1; private final int totalPageNumber; private final int totalResultsNumber; private final int beforeGroupPageNumber; private final int afterGroupPageNumber; public PageImpl(final List results, final int pageNumber, final int resultsPerPage, final int pagePerGroup,final int totalResultsNumber) { super(); this.results = results; this.pageNumber = pageNumber; this.resultsPerPage = resultsPerPage; this.pageGroupNumber = pagePerGroup; this.totalResultsNumber = totalResultsNumber; if(0 == totalResultsNumber % resultsPerPage){ this.totalPageNumber = totalResultsNumber/resultsPerPage; }else{ this.totalPageNumber = totalResultsNumber/resultsPerPage + 1; } this.pagePerGroup = pagePerGroup; if(pageNumber * resultsPerPage >= totalPageNumber){ this.last = true; }else{ this.last = false; } if(pageNumber == 1){ this.first = true; }else{ this.first = false; } if(pageNumber <= pagePerGroup){ this.firstGroup = true; }else{ this.firstGroup = false; } if(0 == totalPageNumber % pagePerGroup){ this.lastGroupNumber = totalPageNumber/pagePerGroup; this.lastGroupPageNumber = lastGroupNumber*pagePerGroup; }else{ this.lastGroupNumber = totalPageNumber/pagePerGroup + 1; this.lastGroupPageNumber = (lastGroupNumber-1)*(pagePerGroup) + 1; } if(0 == pageNumber % pagePerGroup){ if(pageNumber/pagePerGroup - 1 == lastGroupNumber - 1){ this.lastGroup = true; }else{ this.lastGroup = false; } }else{ if(pageNumber/pagePerGroup == lastGroupNumber - 1){ this.lastGroup = true; }else{ this.lastGroup = false; } } if(0 == pageNumber % pagePerGroup){ this.groupNumber = pageNumber/pagePerGroup; }else{ this.groupNumber = pageNumber/pagePerGroup + 1; } if(lastGroup){ pageGroup = new int[totalPageNumber % pagePerGroup]; int allReNo = this.totalPageNumber; for(int i=pageGroup.length;i>0;i--){ pageGroup[i-1] = allReNo--; } }else{ pageGroup = new int[pagePerGroup]; for(int i=0;i<pagePerGroup;i++){ pageGroup[i] = (groupNumber-1)*pagePerGroup + 1 + i; } } this.lastPageNumber = totalPageNumber; if(this.firstGroup){ this.beforeGroupPageNumber = 1; }else{ this.beforeGroupPageNumber = (this.groupNumber-1)*this.pagePerGroup - (this.pagePerGroup-1); } if(this.lastGroup){ this.afterGroupPageNumber = this.lastGroupPageNumber; }else{ this.afterGroupPageNumber = (this.groupNumber+1)*this.pagePerGroup - (this.pagePerGroup-1); } } public int getAfterGroupPageNumber() { return afterGroupPageNumber; } public int getBeforeGroupPageNumber() { return beforeGroupPageNumber; } public int getGroupNumber() { return groupNumber; } public int getPagePerGroup() { return pagePerGroup; } public int getTotalResultsNumber() { return totalResultsNumber; } public int getFirstPageNumber() { return firstPageNumber; } public int getLastPageNumber() { return lastPageNumber; } public int getLastGroupPageNumber() { return lastGroupPageNumber; } public int getFirstGroupPageNumber() { return firstGroupPageNumber; } public int[] getPageGroup() { return pageGroup; } public int getResultsPerPage() { return resultsPerPage; } public boolean isFirst() { return first; } public boolean isFirstGroup() { return firstGroup; } public boolean isLast() { return last; } public boolean isLastGroup() { return lastGroup; } public int getPageGroupNumber() { return pageGroupNumber; } public int getPageNumber() { return pageNumber; } public List getResults() { return results; } public int getTotalPageNumber() { return totalPageNumber; } public int getFirstGroupNumber() { return firstGroupNumber; } public int getLastGroupNumber() { return lastGroupNumber; } }
页面上的处理
<script language="javascript"> function initial(init){ var pageno = document.getElementById('trresearch.pageNo'); if (pageno) { pageno.value = init; }; document.form.submit(); } </script> <html:hidden property="pageNo"/> <html:hidden property="pageOrder"/> <html:hidden property="fuzzyQuery"/> <a href="javaScript:initial('1')">第一页</a> <a href="javaScript:initial('${page.beforeGroupPageNumber }')">前一组</a> <c:forEach items="${page.pageGroup}" var="pageNo"> <c:if test="${pageNo eq page.pageNumber}"> <span class="当前页"> <a href="javaScript:initial('${pageNo}')">${pageNo}</a> </span> </c:if> <c:if test="${pageNo ne page.pageNumber}"> <a href="javaScript:initial('${pageNo}')">${pageNo}</a> </c:if> </c:forEach> <a href="javaScript:initial('${page.afterGroupPageNumber }')">后一组</a> <a href="javaScript:initial('${page.lastPageNumber }')">最后页</a>
相关推荐
基于PaddleOCR开发懒人精灵文字识别插件,使用方式可以查看该文章https://blog.csdn.net/YY007H/article/details/128247582
它们主要专注于视觉设计,因此在实际项目中,你可能需要结合JavaScript或者其他前端框架(如jQuery、Vue.js或React.js)来实现分页的逻辑部分。 总的来说,"30种CSS分页效果经典配色方案源码"是一个宝贵的资源库,...
懒人精灵懒人精灵懒人精灵懒人精灵懒人精灵懒人精灵
"page懒人万能分页(梁玉龙版)struts"可能是指该分页组件的一个特定版本,由梁玉龙开发,并针对Struts框架进行了优化。Struts是一个基于MVC设计模式的Java Web框架,它负责处理用户的请求并转发到相应的Action,...
5. 懒加载分页:懒人图库.txt可能介绍了一种“懒加载”分页技术,即只加载当前可见页面的内容,当用户滚动到页面底部时再加载下一页。这种方式优化了加载性能,特别适用于大型数据集。 6. 自适应分页:随着移动设备...
基于springboot大学生智能消费记账系统的设计与实现.docx
6. **图像识别与处理**:在某些场景下,基于图像识别的自动化可能更为实用,懒人插件可能集成了图像识别技术,帮助用户识别和点击屏幕上的图像元素。 7. **文件操作与网络通信**:懒人插件可能增加了对文件系统和...
标题中的“懒人图库出品的PSD格式分页样式图V2[2008_01_09更新]”表明这是一个由懒人图库制作并发布的资源包,内容涉及PSD(Photoshop Document)格式的分页样式设计。这里的“V2”代表这是该系列产品的第二个版本,...
基于微信小程序懒人美食帮小程序设计与实现.docx
作为办公室一族,您是否越来越感觉逐渐成为电脑的奴隶?本人是感觉深受其害,每天面对电脑没完没了,很多时候面对复杂的操作感觉真累。懒人工具箱是一款免费的...我们坚信懒人创造了世界,让懒人摆脱重复性的无谓劳动
"懒人许工具.7z"是一款针对CAD(计算机辅助设计)软件的便捷插件集合,主要用于提升用户在使用CAD时的工作效率。该工具可能包含了多个版本,适用于不同的CAD平台,如2004年至2010年的版本。从压缩包的文件名来看,...
-天堂的生活.url`和`LAYABOUT'S GALLERY 懒人图库.url`,这些链接可能是指向不同分页的静态页面,点击后跳转到新的URL以展示不同内容。这是早期网页分页的常见方法,但可能导致浏览器历史记录过多。 3. **JSpage...
【标题】懒人精灵源码 懒人精灵源码是一个重要的软件或插件项目,它可能涉及编程语言、软件工程、自动化工具等多个IT领域的知识点。从"懒人精灵"这个名字可以推测,它可能是为了简化用户操作,提高效率而设计的一款...
不跳转页面分页工具,依赖于jQuery,默认样式依赖于bootstrap3.0,通过最大行数和显示 行数计算出分页总数,通过设置的样式结构决定前台展示出现的样式,目前存在两种展出模式,可通过定义扩展方式,增加展出样式。
A light,网盘文件,本人亲测可用,连接永久有效 docker ubuntu. docx Docker Chrome Node. docx PNET 4.0.2 SD-WAN Lab_For- Dummies _By_ Doge. ova ...he docker wireshark.docx Ubuntu Desktop....
基于Spring Boot实现的懒人美食帮微信小程序,旨在为忙碌的现代人提供便捷、快速的美食订餐和美食推荐服务。以下是该小程序的主要功能: 美食浏览与搜索:用户可以在小程序中浏览各类美食,包括餐厅推荐、特色菜品...
本系列为懒人版工具箱,注解丰富,逻辑清晰,移植方便,便于学习和使用,初次上手请参考示例代码。
在懒人办公插件中,LazyOffice.dll提供了插件的所有功能实现,当用户在Excel、Word或ET表格中调用插件时,系统会加载这个DLL文件来执行相应的命令。 总的来说,懒人办公插件V15.0106.0.11通过COM接口实现了与主流...
个人作品,模仿的懒人天气,实现国内地级市的天气查询