sql 语句中where 1=1 1=2作用

写一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值