网页前端多条件搜索 后台分页方法代码




      多条件查询分页方法:
       public Map fenye(Map paramMap) {
// 第几页
Integer page = (Integer) paramMap.get("page");
Integer size = (Integer) paramMap.get("size");

// 取值
int empId = -1; // 员工编号
String empIdString = (String) paramMap.get("empId");
if (empIdString != null && empIdString.length()>0)
empId = Integer.parseInt(empIdString);

// 名称
String empName = (String) paramMap.get("empName");

// 性别
int empSex = -1;
String empSexString = (String) paramMap.get("empSex");
if (empSexString != null && empSexString.length()>0)
empSex = Integer.parseInt(empSexString);

// 部门
int depId = -1;
String depIdString = (String) paramMap.get("depId");
if (depIdString!=null && depIdString.length()>0)
  depId = Integer.parseInt(depIdString);

// 时间
String startAgeString = (String) paramMap.get("startAge");
String endAgeString = (String) paramMap.get("endAge");
Date startAge = null;
Date endAge = null;
if (startAgeString != null && startAgeString.length()>0) {
startAge = java.sql.Date.valueOf(startAgeString);
}
if (endAgeString != null && endAgeString.length()>0) {
            endAge   = java.sql.Date.valueOf(endAgeString);
}
//拼hql语句
String hqlCount = "select count(*) from Emp where 1=1 ";
String hql = "from Emp where 1=1 ";

List values = new ArrayList();
if (empId!=-1)
{
hql = hql + " and empId=?";
hqlCount = hqlCount +" and empId=?";
values.add(empId);
}
if (empName!=null && empName.length()>0 ){
hql = hql + " and empName like ?";
hqlCount = hqlCount +" and empName like ?";
values.add(empName+"%");
}

if (empSex!=-1)
{
hql = hql + " and empSex=?";
hqlCount = hqlCount +" and empSex=?";
values.add(empSex);
}

if (depId!=-1)
{
hql = hql + " and dep.depId=?";
hqlCount = hqlCount +" and dep.depId=?";
values.add(depId);
}

if (startAge!=null && endAge!=null){
hql = hql + " and empBirthday between ? and ?";
hqlCount = hqlCount +" and empBirthday between ? and ?";
values.add(startAge);
values.add(endAge);
}
else if (startAge!=null && endAge==null){
hql = hql + " and empBirthday >= ?";
hqlCount = hqlCount +" and empBirthday >=?";
values.add(startAge);
}
else if (startAge==null && endAge!=null){
hql = hql + " and empBirthday <= ?";
hqlCount = hqlCount +" and empBirthday <=?";
values.add(endAge);
}




//设?号
Query query = getSession().createQuery(hql);
Query queryCount = getSession().createQuery(hqlCount);
if (values.size()>0){
for (int i=0;i<values.size();i++){
queryCount.setParameter(i, values.get(i));
query.setParameter(i, values.get(i));
}
}
// 总条数
Object countObject =queryCount.uniqueResult();
int sum = Integer.parseInt(countObject.toString());
// 总页数
int count = sum % size == 0 ? sum / size : sum / size + 1;
// 越界检查
if (page < 1)
page = 1;
if (page > count)
page = count;
// 查


query.setFirstResult((page - 1) * size).setMaxResults(size);
List list = query.list();

// 保存到map
Map resultMap = new HashMap(0);
resultMap.put("list", list);// 内容
resultMap.put("page", page);// 当前页数
resultMap.put("size", size);// 大小
resultMap.put("count", count);// 总页数
resultMap.put("sum", sum);// 总条数

return resultMap;
}


你可能感兴趣的:(sql,map,fenye)