论坛首页 Java企业应用论坛

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

浏览 4950 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2011-07-27  



      多条件查询分页方法:
       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;
}


   发表时间:2011-09-20  
为什么不用面向对象的方法,把有关查询结果的数目,页码,当前页和结果集封装成一个类,条件页可以封装成一个类,这样不是更好
0 请登录后投票
   发表时间:2011-09-21  
用 MAP 的话 个人觉得更直接,以键值的形式获取 取值更方便
1 请登录后投票
   发表时间:2011-09-27  
hehe198504 写道
为什么不用面向对象的方法,把有关查询结果的数目,页码,当前页和结果集封装成一个类,条件页可以封装成一个类,这样不是更好

+1
0 请登录后投票
   发表时间:2011-10-22  
很有想法啊,嘿嘿,看着不错啊
0 请登录后投票
   发表时间:2011-10-24  
如果需要动态SQL,用模板语言就搞定了;如果需要自定义类来解析,抽象出一个通用性的where类就OK了~
1 请登录后投票
   发表时间:2011-10-24  
yn5411 写道
如果需要动态SQL,用模板语言就搞定了;如果需要自定义类来解析,抽象出一个通用性的where类就OK了~

用模板语言终归还是要手写ifelse,要自动化才好啊
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics