最近由于项目需要,自己尝试做了一个组合查询+分页的组件。花了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(时间仓促没来得及写验证....)
id dept job costType state belongUserId dateRange moneyRange startDate startMoney endDate endMoney checkName orderByDate
接下来是编写针对某个模型的组合查询映射文件,例如材料报销单
id id startDate date endDate date dept dept job job dateRange date state state checkName name moneyRange requestMoney startMoney requestMoney endMoney requestMoney orderByDate date
然后运行test包下的TestConditionFinderImpl
/** * 测试 * @author fw * */ public class TestConditionFinderImpl extends BaseSpringTest { private ConditionFinder conditionFinder; private PageBeanpb; 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 (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 <= ?