1.页面点击查询所有
int num = 1;
int rowCount = 0;
int size = 10;//定义分页的每页显示的初始个数
if(str_num != null && !"".equals(str_num)) {
num = Integer.parseInt(str_num);
}
if(str_size != null && !"".equals(str_size)){
size = Integer.parseInt(str_size);
}
List<Param> params = new ArrayList<Param>();
Map<String, String> orders = new HashMap<String, String>();
//--------设置查询条件开始----------
if(sktname!=null && !"".equals(sktname.trim())){//课题名称
Param param1 = new Param("kt.ktName1","like",sktname);
params.add(param1);
}
if(sktprojectname!=null && !"".equals(sktprojectname.trim())){//所属专项名称
Param param2 = new Param("kt.ktProjectName","like",sktprojectname);
params.add(param2);
}
if(sktcommission!=null && !"".equals(sktcommission.trim())){//课题委托单位
Param param3 = new Param("kt.ktCommissionUnit","like",sktcommission);
params.add(param3);
}
if(sktundertaker!=null && !"".equals(sktundertaker.trim())){//承担单位
Param param4 = new Param("kt.ktUndertakeUnitName1","like",sktundertaker);
params.add(param4);
}
if(sktleader!=null && !"".equals(sktleader.trim())){//负责人
Param param5 = new Param("kt.ktLeader","like",sktleader);
params.add(param5);
}
//创建时间
if(sdate1 != null && !"".equals(sdate1.trim()) && sdate2 != null && !"".equals(sdate2.trim())){
Param param6 = new Param("kt.ktUpdateDate","between",sdate1+","+sdate2);
params.add(param6);
}else{
if(sdate1 != null && !"".equals(sdate1.trim())){
Param param7 = new Param("kt.ktUpdateDate",">=",sdate1);
params.add(param7);
}else if(sdate2 != null && !"".equals(sdate2.trim())){
Param param8 = new Param("kt.ktUpdateDate","<=",sdate2);
params.add(param8);
}
}
if(status!=null && !"".equals(status.trim())){//审核状态
Param param9 = new Param("kt.ktStatus","=",status);
params.add(param9);
}
//降序排序
orders.put("kt.ktUpdateDate", "desc");
//----------设置条件结束------------------
rowCount = Integer.parseInt(this.tblDjKtReportCheckService.getObjectList(params, orders, 0, 0).toString());//页总数
Page page = new Page(size,num,rowCount);
List<TblDjKtReport> list = (List<TblDjKtReport>) this.tblDjKtReportCheckService.getObjectList(params, orders, page.getStartRow(), page.getSize());//分页查询的记录数
2.Param 类
/**
* 2010-11-08
* remark: 查询参数辅助类
* author: 001
*/
public class Param {
private String paramName;//参数名称
private Object paramValue;//参数值
private String operator;//连接符(如:=,like,in)
public Param() {}
public Param(String paramName,String operator,Object paramValue) {
this.paramName = paramName;
this.paramValue = paramValue;
this.operator = operator;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getParamName() {
return paramName;
}
public void setParamName(String paramName) {
this.paramName = paramName;
}
public Object getParamValue() {
return paramValue;
}
public void setParamValue(Object paramValue) {
this.paramValue = paramValue;
}
}
3.dao层实现方法
/**
* 查询分页列表通用方法(课题总局报告)
* @param params 参数集合
* @param orders 排序条件
* @param start 起始条数
* @param count 页大小
* @return List<AdminUserBean>
* @throws Exception
*/
public Object getObjectList(List<Param> params, Map orders,int start, int count) throws Exception{
Session session = null;
Query query = null;
try {
// 组合检索语句
String strSQL = null;
StringBuffer strBuff = new StringBuffer();
if(count > 0){
strBuff.append("from TblDjKtReport kt where kt.isDele='0' and kt.ktIsSubmit='1'");
}else{
strBuff.append("select count(*) from TblDjKtReport kt where kt.isDele='0' and kt.ktIsSubmit='1'");
}
if (params != null && params.size() > 0){
for (Param param : params) {
strBuff.append(" and ");
if (param.getOperator().trim().equals("in")) {
// strBuff.append(param.getParamName());
// strBuff.append(" " + param.getOperator() + " (");
// strBuff.append(param.getParamValue());
// strBuff.append(")");
}else if (param.getOperator().trim().equals("like")) {
strBuff.append(param.getParamName());
strBuff.append(" " + param.getOperator());
strBuff.append(" '%" + param.getParamValue()+"%'");
} else if (param.getOperator().trim().equals("between")){
strBuff.append(param.getParamName());
strBuff.append(" " + param.getOperator());
String date[] = param.getParamValue().toString().split(","); //2000-01-01,2000-02-02 格式化成数组
strBuff.append(" '"+date[0]+" 00:00:00'"+" and '"+date[1]+" 23:59:59'");
} else {
strBuff.append(param.getParamName());
strBuff.append(param.getOperator());
strBuff.append("'" + param.getParamValue()+"'");
}
}
}
if (orders != null && !orders.isEmpty()) {
strBuff.append(" order by ");
for (Iterator it = orders.keySet().iterator(); it.hasNext();) {
String orderName = (String) it.next();
strBuff.append(orderName);
strBuff.append(" ");
strBuff.append((String) orders.get(orderName));
strBuff.append(",");
}
strBuff.setLength(strBuff.length() - 1);
}
strSQL = new String(strBuff);
session = getHibernateTemplate().getSessionFactory().openSession();
System.out.println("第一次SQL:"+strSQL);
query = session.createQuery(strSQL);
if(count > 0){
if(start>=0){
query.setFirstResult(start);
query.setMaxResults(count);
}
return query.list();
}else{
return Integer.parseInt(query.list().get(0).toString());
}
} catch (HibernateException e){
Loggers.error("查询管理用户信息错误:TblDjKtReportDaoImpl getObjectList"+e);
throw new HibernateException(e.getMessage());
}finally{
if(session != null && session.isOpen()){
session.clear();
session.close();
}
}
}