最近由于项目需要,自己尝试做了一个组合查询+分页的组件。花了5天时间,只能说是初具雏形。核心还是hibernate的QBC,只是加上了XML实现了可配置。能够满足当前项目地应用,也解决了criteria查询记录条数的BUG,有兴趣的朋友可以下载使用。
使用的例子和源码以及说明见附件。由于时间仓促,今后有空的话还会继续进行修改,提供JDBC支持,以及脱离SPRING和HIBERNATE等等。
基本使用方法如下,根据项目需求建立自己的通用条件查询对象并继承CommonCondition
/**
* 本项目的通用条件查询对象 包含cret所有页面的条件作为属性 今后可能继续增加属性
* @author fw
*
*/
public class CretCondition extends CommonCondition{
private Integer id;//xx编号
private String dept;//部门
private String job;//岗位
private String checkName;//单据经办人
private String costType;//报销单类型
private Integer state;//单据状态
private Date startDate;//起始时间
private Date endDate;//结束时间
private Date[] dateRange;//日期范围
........
}
然后编写一个总的条件配置文件,默认放src目录下,取名为condition-cfg.xml(时间仓促没来得及写验证....)
<?xml version="1.0" encoding="UTF-8"?>
<condition-cfg common-condition-class="com.lovo.cett.model.condition.CretCondition">
<!-- 为需要组合条件查询的模型编写的配置文件 默认相对目录为src 可以有多个 -->
<mapping resource="config/condition/stuffcost-condition-mapping.xml"/>
<mapping resource="config/condition/tourcost-condition-mapping.xml"/>
<!-- 配置common-condition-class中的属性和运算符号的对应关系 -->
<expression-mapping>
<equal>
<property>id</property>
<property>dept</property>
<property>job</property>
<property>costType</property>
<property>state</property>
<property>belongUserId</property>
</equal>
<between>
<property>dateRange</property>
<property>moneyRange</property>
</between>
<greater-than>
<property>startDate</property>
<property>startMoney</property>
</greater-than>
<less-than>
<property>endDate</property>
<property>endMoney</property>
</less-than>
<like>
<property>checkName</property>
</like>
<!-- 默认按模型的id属性降序排列 如果不需要排序的话 可以写一个空的<order-by/>-->
<order-by defaultProperty="id" defaultOrder="desc">
<!-- 配置通用条件对象中的orderByDate属性为动态排序字段 -->
<property>orderByDate</property>
</order-by>
</expression-mapping>
</condition-cfg>
接下来是编写针对某个模型的组合查询映射文件,例如材料报销单
<?xml version="1.0" encoding="UTF-8"?>
<condition-mapping mapping-class="com.lovo.cett.model.cost.StuffCost" >
<!-- 动态设置模型类中抓取策略 暂时只能动态设置join/select/auto
auto表示不改变抓取策略 即使用hbm文件中配置的抓取策略 可以配置多个-->
<model-fetch property="stuffCostItems" fetch="select"/>
<!-- 关联CretCondition通用条件对象和StuffCost对象的属性 -->
<property>
<condition-property>id</condition-property>
<model-property>id</model-property>
</property>
<property>
<condition-property>startDate</condition-property>
<model-property>date</model-property>
</property>
<property>
<condition-property>endDate</condition-property>
<model-property>date</model-property>
</property>
<property>
<condition-property>dept</condition-property>
<model-property>dept</model-property>
</property>
<property>
<condition-property>job</condition-property>
<model-property>job</model-property>
</property>
<property>
<condition-property>dateRange</condition-property>
<model-property>date</model-property>
</property>
<property>
<condition-property>state</condition-property>
<model-property>state</model-property>
</property>
<property>
<condition-property>checkName</condition-property>
<model-property>name</model-property>
</property>
<property>
<condition-property>moneyRange</condition-property>
<model-property>requestMoney</model-property>
</property>
<property>
<condition-property>startMoney</condition-property>
<model-property>requestMoney</model-property>
</property>
<property>
<condition-property>endMoney</condition-property>
<model-property>requestMoney</model-property>
</property>
<property>
<condition-property>orderByDate</condition-property>
<model-property>date</model-property>
</property>
</condition-mapping>
然后运行test包下的TestConditionFinderImpl
/**
* 测试
* @author fw
*
*/
public class TestConditionFinderImpl extends BaseSpringTest {
private ConditionFinder conditionFinder;
private PageBean<StuffCost> pb;
private CretCondition cc;
static final int NOW_PAGE=1;//当前第1页
static final int PAGE_SIZE=3;//每页显示3条
static final int TOTAL_COUNT=7;
@Override
protected void onSetUp() throws Exception {
/**以下条件代表:查询 经办人like fw/状态等于1/日期为昨天到今天/申请金额为100至200
* 的材料报销单 结果按日期降序
* 每页显示3条 显示第1页
* */
pb = new PageBean<StuffCost>(NOW_PAGE,PAGE_SIZE);
cc = new CretCondition();
cc.setState(1);
cc.setCheckName("fw");
Date endDate = new Date();
Date startDate = new Date();
startDate.setDate(startDate.getDate()-1);
cc.setDateRange(new Date[]{startDate,endDate});
cc.setMoneyRange(new Double[]{Double.parseDouble("100"),Double.parseDouble("200")});
cc.setOrderByDate(OrderBy.DESC);
........................
以ORACLE为例,生成SQL如下
Hibernate: select count(*) as y0_ from t_cost this_ where this_.c_type='sc' and this_.c_name like ? and this_.c_state=? and this_.c_date between ? and ? and this_.c_requestMoney between ? and ?
Hibernate: select * from ( select this_.c_id as c1_2_0_, this_.c_costdept as c3_2_0_, this_.c_date as c4_2_0_, this_.c_dept as c5_2_0_, this_.c_financeSanctionMopney as c6_2_0_, this_.c_flowchoice as c7_2_0_, this_.c_job as c8_2_0_, this_.c_loanmoney as c9_2_0_, this_.c_loanperson as c10_2_0_, this_.c_name as c11_2_0_, this_.c_remark as c12_2_0_, this_.c_requestMoney as c13_2_0_, this_.c_tourProject as c14_2_0_, this_.c_state as c15_2_0_ from t_cost this_ where this_.c_type='sc' and this_.c_name like ? and this_.c_state=? and this_.c_date between ? and ? and this_.c_requestMoney between ? and ? order by this_.c_date desc, this_.c_id desc ) where rownum <= ?
分享到:
相关推荐
基于struts2+hibernate3+spring的分页组件
SSH(Spring+Struts+Hibernate)轻量级论坛Forum演示系统(Java Web Edition): Spring+Struts+Hibernate框架、Pagination分页组件、JQuery、JSON、Ajax、DIV+CSS、JavaScript、HTML5。另建议使用相当于IE8.0以上的...
SSH(Spring+Struts+Hibernate)轻量级购物软件Shopping演示系统(Java Web Edition)技术要点:Spring+Struts+Hibernate三大框架整合,Jquery-treeview、DIV+CSS3、HTML5、JavaScript、分页组件pagination、MySQL。
自己写了个分页组件.(不是太好看...).现在ajax都已经不是什么新鲜东东了..我在里面配的是dwr..(这个简单些..其他的我看着晕...).没有自己写页面输出.我使用的table组件是:eXtremeComponents.自我感觉这个组件比较好....
J2EE三大框架_笔记 共分三块: J2EE框架_笔记_a: 1-JSP+JDBC_假分页笔记 2-JSP+JDBC_真分页(基于Oracle数据库...53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + Spring + Hibernate笔记
J2EE三大框架_笔记 共分三块: J2EE框架_笔记_a: 1-JSP+JDBC_假分页笔记 2-JSP+JDBC_真分页(基于Oracle数据库...53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + Spring + Hibernate笔记
Spring Data JPA的底层是基于Hibernate的,其JpaRepository、PageAndSortingRepository、CrudRepository等组件能够实现快速单表读取与后端分页、排序等功能,所以其便捷性很适合单体式系统的开发。 * 权限管理使用...
J2EE三大框架_笔记 共分三块: J2EE框架_笔记_a: 1-JSP+JDBC_假分页笔记 2-JSP+JDBC_真分页(基于Oracle数据库...53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + Spring + Hibernate笔记
站静态化,网上内容采集功能超强的CMS(1) 注释语法.chm(1) struts2+hibernate3.2+spring2.5(1) Red+Hat+Enterprise(1) 软件(1) struts分页组件(1) Hibernate(1)
Spring Data JPA的底层是基于Hibernate的,其JpaRepository、PageAndSortingRepository、CrudRepository等组件能够实现快速单表读取与后端分页、排序等功能,所以其便捷性很适合单体式系统的开发。 * 权限管理使用...
53使用Struts + Spring + Hibernate完成分页笔记.pdf 54留言管理程序_Struts + Spring + Hibernate笔记.pdf 6-Struts标签-BEAN标签笔记.pdf 7-Struts标签 -LOGIC标签笔记.pdf 8-Struts标签- HTML标签笔记.pdf 9-...
Spring提供了DAO支持,可以简化DAO组件的开发,特别是IoC容器的使用,提供了DAO组件与业务逻辑组件之间的松耦合组合方式。 所有的DAO组件,都由容器负责注入到业务逻辑组件中,使用业务逻辑组件无需关心DAO组件的...
基于Spring4框架的三层架构开发完成。 3. Mybatis版本为:3.2.8。 4. 使用了SpringMVC框架,Spring4的xml配置和注解配置。 5. 前台展示页面使用了JQuery EasyUI的布局、列表和消息组件。 6. 系统实现了较为完善...
struts2+spring2.5+hibernate3.2整合所需的所有jar包,并额外包含了dwr.jar、分页组件pager-taglib、绘图组件jfreechart.jar、Excel解析工具包jxl.jar、数据库连接池工具包proxool.jar
(2) 超级用户授权论坛版块管理员;(3) 论坛版块管理和维护;... 二、使用技术:(1) Spring+SpringMVC+Hibernate整合框架;(2) HibernateTemplate模板;...(4) JSTL、JSP tagment、分页组件; (5) Tomcat + MySQL。
rapid-validation 集成extremeTable列表分页组件,并简单扩展显示pageNumber 集成DBUnit及spring对数据库测试的支持 内置的分页查询PageInfo接口 Java日期转换的特殊处理,与My97DatePicker集成 内置最精简的ant构建...
1-JSP+JDBC_假分页笔记.pdf 2-JSP+JDBC_真分页(基于Oracle数据库分页)笔记.pdf 3-JSP+DAO和MVC+DAO(基于MySQL数据库分页)-v笔记.pdf 4-Struts入门笔记.pdf ...54留言管理程序_Struts + Spring + Hibernate笔记.pdf
这个项目是一个基于Java语言开发的Web应用程序,采用SSM(Spring+SpringMVC+MyBatis)或SSH(Spring+SpringMVC+Hibernate)框架进行开发,使用MySQL作为数据存储,JSP作为页面开发。 项目的目标是构建一个高效、可靠...