50010623 发表于 2018-10-22 11:54:09

对sql的查询语句做成对象式,简单实现。Where部分

package net.zz.zjf.plugin;  

  
import java.lang.reflect.Array;
  
import java.util.*;
  
import java.util.regex.Matcher;
  
import java.util.regex.Pattern;
  

  
/**
  
* Created by ZaoSheng on 2015/8/5.
  
*/
  
public class Where implements SQLParams{
  
    //用于存放第一个条件的名
  
    private String first = null;
  
    /*这里存放所有的where需要用到的东西。wheres的key是property名,value主要分为三部分
  
    *value主要分为三部分,value用来存放property的值,在BETWEEN的情况下value是数组
  
    *数组长度为2。value存放AndOr,value存放具体的where操作,比如 ">",">=","like","in"
  
    *等操作。
  
    */
  
    private Map wheres = new HashMap();
  
    //这个对象用来存放具体的属性值attrs的key是sql中":property"中的"property",value是对应的值
  
    private Map attrs = new HashMap();
  
    //这个集合不一定有值,在执行toFormatSQL方法后才会有值。存放的值依次对应sql中的第几个"?"
  
    private List paras = new ArrayList();
  

  
    public Where() {
  

  
    }
  

  
    public Where(String propertyName, Object value, AndOr andor, Restriction restriction) {
  
      first = propertyName;
  
       add(propertyName, value, andor, restriction);
  
    }
  

  
    public Where(String propertyName, Object value, AndOr andor) {
  
      this(propertyName, value, andor, Restriction.EQ);
  
    }
  

  
    public Where(String propertyName, Object value, Restriction restriction) {
  
      this(propertyName, value, AndOr.NUL, restriction);
  
    }
  

  
    public Where(String propertyName, Object value) {
  
      this(propertyName, value, Restriction.EQ);
  
    }
  

  
    public Map getWheres() {
  
      return wheres;
  
    }
  

  
    public Map getAttrs() {
  
      return attrs;
  
    }
  

  
    public List getParas() {
  
      return paras;
  
    }
  

  
    public Where and(String propertyName, Object value, Restriction restriction) {
  
      add(propertyName, value, AndOr.AND, restriction);
  
      return this;
  
    }
  

  
    public Where and(String propertyName, Object value) {
  
      return and(propertyName, value, Restriction.EQ);
  
    }
  

  
    public Where or(String propertyName, Object value, Restriction restriction) {
  
      add(propertyName, value, AndOr.OR, restriction);
  
      return this;
  
    }
  

  
    public Where or(String propertyName, Object value) {
  
      return or(propertyName, value, Restriction.EQ);
  
    }
  

  
    protected void add(String key, Object value, AndOr andor, Restriction restriction) {
  
      if (null == value || "".equals(value)) {
  
            if (key.equals(first))
  
            {
  
                first = null;
  
            }
  
            wheres.remove(key);
  
      } else {
  
            wheres.put(key, new Object[]{value, andor, restriction});
  
      }
  

  
    }
  

  

  

  
    public String toSQL() {
  
      if (wheres.isEmpty()) return "";
  
      StringBuilder sb = new StringBuilder();
  
      if (null != first)setSql(first, wheres.get(first), sb );
  

  
      for (String key : wheres.keySet()) {
  
            if (key.equals(first))continue;
  

  
            Object[] objects = wheres.get(key);
  
            setSql(key, objects, sb);
  

  
      }
  
      return sb.toString();
  
    }
  

  
    /**
  
    *这个方法之所以提取出来是因为能更好的在多个地方引用(应该用代码重构的方式来讲的,可惜我不会讲).
  
    */
  
    private void setSql(String key, Object[] objects, StringBuilder sb)
  
    {
  
      AndOr andOr = (AndOr) objects;
  
      Restriction restriction = (Restriction)objects;
  
      switch (restriction) {
  
            case LIKE:
  
            case LLIKE:
  
            case RLIKE:
  
                sb.append(andOr.toMatchString(key, "like :" + key));
  
                attrs.put(key, restriction.toMatchString(objects.toString()));
  
                break;
  
            case NULL:
  
            case NOTNULL:
  
                sb.append(andOr.toMatchString("", restriction.toMatchString(key)));
  
                break;
  
            case BETWEEN:
  
                sb.append(andOr.toMatchString(key, restriction.toMatchString(key)));
  
                Object[] value = (Object[]) objects;
  
                attrs.put(String.format("%s1", key),value);
  
                attrs.put(String.format("%s2", key),value);
  
                break;
  
            default:
  
                sb.append(andOr.toMatchString(key, restriction.toMatchString(key)));
  
                attrs.put(key, objects);
  
      }
  
    }
  

  
    @Override
  
    public String toFormatSQL() {
  
      returntoFormatSQL(toSQL(), attrs, paras);
  
    }
  

  
    /**
  
   * @param whereSQL
  
   * @param attrs
  
   * @param values
  
   * @return
  
   */
  
    public static String toFormatSQL(String whereSQL, Map attrs, List values) {
  
      Matcher matcher = Pattern.compile(":(\\w+)").matcher(whereSQL);
  
      String rexp = null;
  
      while (matcher.find()) {
  
            String group = matcher.group(1);
  
            Object ov = attrs.get(group);
  
            if (ov instanceof List) {
  
                StringBuilder sb = new StringBuilder();
  
                List vs = (List) ov;
  
                for (Object v : vs) {
  
                  sb.append("?,");
  
                  values.add(v);
  
                }
  
                sb.deleteCharAt(sb.length() - 1);
  
                rexp = sb.toString();
  

  
            } else {
  
                values.add(ov);
  
                rexp = "?";
  
            }
  
            whereSQL = whereSQL.replace(String.format(":%s", group), rexp);
  
      }
  
      return whereSQL;
  
    }
  

  
    @Override
  
    public String toString() {
  
      return toFormatSQL();
  
    }
  

  
    public static void main(String[] args) {
  
      Calendar c = new GregorianCalendar();
  

  
      Where where = new Where("name", "张三");
  
      where.or("class", 2);
  
      where.and("sex", true);
  
      where.and("age", new Integer[]{1,10}, Restriction.BETWEEN);
  
      List ids = new ArrayList();
  
      ids.add(4);
  
      ids.add(3);
  
      where.and("id", ids, Restriction.IN);
  
      System.out.println(where.toString());
  

  
      for (Object value :where.getParas())
  
      {
  
            System.out.print(String.format("%s ", value));
  
      }
  

  

  

  
    }
  
}


页: [1]
查看完整版本: 对sql的查询语句做成对象式,简单实现。Where部分