写一个1=1是为了后面的条件。这个SQL应该是在程序中拼出来的,程序中首先不能肯定后面的条件是否肯定会有,为了程序简单先加上where 1=1,后面的就可以直接拼接。如果不这样处理就需要在拼接后面的每一个条件时都要判断是不是where子句的第一个件条,以决定是否要在前面加and
public PageResults<OrderVo> find(Long oid, String name, String mobile, String ostatus, Date time1, Date time2) { this.userSessionFactoryCustomer(); this.userSessionFactoryOrder(); String queryString = "select o.id as id ,o.status as ostatus,o.createDate as createDate ,o.paidDate as paidDate ,o.collectionToolSendDate as collectionToolSendDate ,o.collectionToolSendLogisticId as collectionToolSendLogisticId,o.finishDate as finishDate ,o.remark as remark ," + "o.amount as amount,o.thirdPartyPayment as paymode,ol.companyName as companyName, " + "c.name as name,c.mobile as mobile,ol.address as address " + "from orders.orders o, customer.customer c,orders.order_logistics ol " + "where o.customerId=c.id and ol.id=o.collectionToolSendLogisticId and o.valid=true "; String queryCountString = "select count(*) from orders.orders o, customer.customer c,orders.order_logistics ol where o.customerId=c.id and ol.id=o.collectionToolSendLogisticId and o.valid=true"; if (oid!=null){ queryString += " and o.id = :oid"; queryCountString += " and o.id = :oid"; } if(name != null && !"".equals(name)){ queryString += " and c.name = :name"; queryCountString += " and c.name = :name"; } if(mobile != null && !"".equals(mobile)){ queryString += " and c.mobile = :mobile"; queryCountString += " and c.mobile = :mobile"; } if( ostatus != null && !"".equals(ostatus)){ queryString += " and o.status = :ostatus"; queryCountString += " and o.status = :ostatus"; } if(time1!=null){ queryString +=" and o.createDate >= :time1 and o.createDate <= :time2"; queryCountString += " and o.createDate >= :time1 and o.createDate <= :time2"; } queryString += " order by o.createDate"; Query query = this.getSession().createSQLQuery(queryString) .addScalar("id",StandardBasicTypes.LONG) .addScalar("ostatus") .addScalar("createDate") .addScalar("paidDate") .addScalar("collectionToolSendDate") .addScalar("collectionToolSendLogisticId",StandardBasicTypes.LONG) .addScalar("finishDate") .addScalar("remark") .addScalar("name") .addScalar("mobile",StandardBasicTypes.STRING) .addScalar("address") .addScalar("amount") .addScalar("companyName") .addScalar("paymode") .setResultTransformer(Transformers.aliasToBean(OrderVo.class)); Query queryCount = this.getSession().createSQLQuery(queryCountString); if (oid!=null){ query.setParameter("oid", oid); queryCount.setParameter("oid", oid); } if(name != null && !"".equals(name)){ query.setParameter("name", name); queryCount.setParameter("name", name); } if(mobile != null && !"".equals(mobile)){ query.setParameter("mobile", mobile); queryCount.setParameter("mobile", mobile); } if(ostatus != null && !"".equals(ostatus)){ query.setParameter("ostatus", ostatus); queryCount.setParameter("ostatus", ostatus); } if(time1!=null){ query.setParameter("time1",time1); query.setParameter("time2",time2); queryCount.setParameter("time1",time1); queryCount.setParameter("time2",time2); } int size = SystemContext.getSize(); int offset = SystemContext.getOffset(); query.setFirstResult(offset).setMaxResults(size); List<OrderVo> olist = query.list(); PageResults<OrderVo> olistpage = new PageResults<OrderVo>(); olistpage.setResults(olist); olistpage.setCurrentPage(offset); olistpage.setPageSize(size); BigInteger totalCount=(BigInteger)queryCount.uniqueResult(); olistpage.setTotalCount(totalCount.intValue()); return olistpage; } }
where 1=2 是让记录集为空,因为只是用来插入一行,只需要得到表的各属性的数据类型即可。
速度较快 因为它不用和表内任何字段比对
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1679353