多条件组合查询
来源:互联网 发布:停车场找车软件 编辑:程序博客网 时间:2024/06/05 02:35
方法一:
//多条件组合查询 public List<Customer> findMoreCondition(Customer customer) { //1-使用hibernate模板里面find方法实现 //拼接hql语句 String hql = "from Customer where 1=1"; //创建list集合,如果值不为空,就把值设置到list里面 List<Object> p = new ArrayList<Object>(); //判断条件值是否为空,如果不为空就拼接 if(customer.getCustName()!=null && !"".equals(customer.getCustName())){ hql += " and custName=?"; p.add(customer.getCustName()); } if(customer.getCustLevel()!=null && !"".equals(customer.getCustLevel())){ hql += " and custLevel=?"; p.add(customer.getCustLevel()); } if(customer.getCustSource()!=null && !"".equals(customer.getCustSource())){ hql += " and custSource=?"; p.add(customer.getCustSource()); } System.out.println("hql:"+hql); System.out.println("list:"+p); return (List<Customer>) this.getHibernateTemplate().find(hql, p.toArray()); }
方法二(使用较多):
public List<Customer> findMoreCondition(Customer customer) { //2.使用离线对象 DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class); //判断条件值是否为空 if(customer.getCustName()!=null&&!"".equals(customer.getCustName())){ //对属性设置值 criteria.add(Restrictions.eq("custName", customer.getCustName())); } /*if(customer.getCustLevel()!=null && !"".equals(customer.getCustLevel())){ criteria.add(Restrictions.eq("custLevel", customer.getCustLevel())); }*/ if(customer.getCustSource()!=null && !"".equals(customer.getCustSource())){ criteria.add(Restrictions.eq("custSource", customer.getCustSource())); } return (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria); }
阅读全文
0 0
- 多条件组合查询
- JDBC多条件组合查询
- 多条件组合查询SQL优化一则
- asp中的多条件组合查询实现
- Linq 的多条件组合查询
- 多条件组合查询的解决方案
- linq to sql 多条件组合查询
- Linq 多条件查询组合问题
- C# MongoDB 多条件动态组合查询
- 多条件组合查询,sql语句连接
- 组合条件查询
- java组合条件查询
- springdataJpa多表查询,多条件组合查询+分页
- lucene查询之 多条件查询 BooleanQuery 可以组合查询条件。及代码示例
- 蛙蛙推荐:asp中的多条件组合查询实现
- 用iBatis实现多条件自由组合查询
- SQL如何构建多条件组合查询,而且不降低效率
- hibernate 多条件组合查询 之 sql 拼接
- 谈下最近接触的zpl语言
- Excel 2016比“分列”还强大千倍的功能
- Day04 Sqlite数据库
- 8月19日CCPC——Party(未解决)
- 应用服务器未安装oracle,安装PLSQL的方法
- 多条件组合查询
- 基于android6.0版本的SELinux文件访问安全策略
- 安卓折线图折线图charts
- Inno Setup 权限问题
- 综合---扩展方法集
- linux系统无网卡配置文件
- session
- 直接交换两个变量的值
- codility FrogJmp