java组合条件查询

public long getOperateLogCounts(Long userId, String day, String ipAddress,
			String description,String operateTypeName, int limit, int offset) {
		  String querySql = "select count(*) from  ir_user.view_login_operate_type  as t ";  
		  StringBuffer sb = new StringBuffer();
			if(userId!=null || day!=null || ipAddress!=null || description !=null || operateTypeName!=null) sb.append(" where ");
		    if(userId!=null){
		    	if(userId == -1){
		    		sb.append("t.userId is null ");
		    	}else{
		    		sb.append("t.userId ="+userId);
		    	}
			} 
			//日期
			if(userId!=null && day!=null) sb.append(" and ");
			if(day!=null) sb.append("t.createTime  BETWEEN'"+day.split("/")[0]+"' and '"+day.split("/")[1]+"'");
			if((userId!=null || day!=null) && ipAddress!=null) sb.append(" and ");
			
			if(ipAddress!=null) sb.append("t.ipAddress like '%"+ipAddress+"%'");
			//描述
			if((userId!=null || day!=null || ipAddress!=null )&& description !=null) sb.append(" and ");
			if(description!=null) sb.append("t.description like '%"+description+"%'");
			if((userId!=null || day!=null || ipAddress!=null || description !=null)&& operateTypeName!=null) sb.append(" and ");
			if(operateTypeName!=null) sb.append("t.operateType ='"+operateTypeName+"'");
      
          Query query = hbCrudDAO.getSessionFactory().getCurrentSession().createSQLQuery((querySql+sb.toString()));  
          BigInteger  countL = 	(BigInteger) query.list().get(0);
          if(countL== null){
        	  return 0;
          }else{
        	  return countL.longValue();
          }
	}


你可能感兴趣的:(java)