• hibernate多条件组合查询的两种方式


    第一种:拼接hql语句方式:

     1 //第一种方式:拼接hql语句实现
     2     public List<Customer> findMoreCondition(Customer customer) {
     3         //拼接hql语句
     4         String hql = "from Customer where 1=1 ";
     5         //创建list集合设置参数值
     6         List<Object> listparam = new ArrayList<Object>();
     7         //判断customer条件值是否为空,如果不为空拼接hql语句
     8         if(customer.getCustName()!=null && !"".equals(customer.getCustName())) {
     9             hql += " and custName=?";
    10             //把值设置到list集合里面
    11             listparam.add(customer.getCustName());
    12         }
    13         if(customer.getCustLevel()!=null && !"".equals(customer.getCustLevel())) {
    14             hql += " and custLevel=?";
    15             listparam.add(customer.getCustLevel());
    16         }
    17         if(customer.getCustSource()!=null && !"".equals(customer.getCustSource())) {
    18             hql += " and custSource=?";
    19             listparam.add(customer.getCustSource());
    20         }
    21         //调用hibernate模板的方法实现查询
    22         List<Customer> list = 
    23                 (List<Customer>) this.getHibernateTemplate().find(hql, listparam.toArray());
    24         return list;
    25     }

    第二种:使用离线对象方式:

     1 //条件查询 离线查询方式
     2     @SuppressWarnings("all")
     3     public List<LinkMan> findmoreCondition(LinkMan linkMan) {
     4         DetachedCriteria criteria = DetachedCriteria.forClass(LinkMan.class);
     5         if(linkMan.getLkmName()!=null && !"".equals(linkMan.getLkmName())){
     6             criteria.add(Restrictions.eq("lkmName", linkMan.getLkmName()));
     7         }
     8         if(linkMan.getCustomer().getCid()!=null && linkMan.getCustomer().getCid()>0){
     9             criteria.add(Restrictions.eq("customer.cid", linkMan.getCustomer().getCid()));
    10         }
    11         return (List<LinkMan>) this.getHibernateTemplate().findByCriteria(criteria);
    12     }
  • 相关阅读:
    django ---解决跨域的问题
    python-isinstance函数
    python每日一学-os模块常用函数
    调用父类方法super
    fiddler小运用-断点
    劝告
    Django model字段
    Jenkins自动化部署前端
    解决react使用antd table组件固定表头后,表头和表体列不对齐以及配置fixed固定左右侧后行高度不对齐
    高德地图判断点的位置是否在浏览器可视区域内
  • 原文地址:https://www.cnblogs.com/cuibin/p/6791404.html
Copyright © 2020-2023  润新知