Example #1
0
  private String makeSQL(T t, String[] fields, String[] values) throws SqlCreateException {
    Class<?> clazz = t.getClass();
    String table = ORMConfigBeanUtil.getTable(clazz);
    ReflectUtil ru = new ReflectUtil(t);
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    String idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
    String idField = ORMConfigBeanUtil.getIdField(clazz);
    Method idGetter = ru.getGetter(idField);
    if (idGetter == null) throw new SqlCreateException("can not find id getter.");

    StringBuilder condition = new StringBuilder();
    try {
      condition.append(idColumn).append(" = '").append(idGetter.invoke(t)).append("'");
    } catch (Exception e) {
      throw new SqlCreateException(idGetter + " invoke exception " + e.toString());
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < columns.length; ++i) {
      String column = columns[i];
      if (sb.length() > 0) sb.append(", ");

      sb.append(column).append(" = '");
      sb.append(values[i]).append("'");
    }

    return String.format("UPDATE %s SET %s WHERE %s ;", table, sb.toString(), condition);
  }
  private Sql makeSQL(T t, String[] fields, Object[] values) throws SqlCreateException {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    String table = ORMConfigBeanUtil.getTable(clazz, false);
    ReflectUtil ru = new ReflectUtil(t);
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    String idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
    String idField = ORMConfigBeanUtil.getIdField(clazz);
    Method idGetter = ru.getGetter(idField);
    if (idGetter == null) throw new SqlCreateException("can not find id getter.");
    Object idValue = null;
    try {
      idValue = idGetter.invoke(t);
    } catch (Exception e) {
      throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e);
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < columns.length; ++i) {
      String column = columns[i];
      if (sb.length() > 0) sb.append(", ");

      //			sb.append(column).append(" = '").append(values[i]).append("'");
      sb.append(column).append(" = ?");
      sql.args.add(values[i]);
    }
    //		String condition = new StringBuilder().append(idColumn).append(" =
    // ").append("'").append(idValue).append("'").toString();
    String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
    sql.args.add(idValue);
    sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, sb.toString(), condition);

    return sql;
  }
Example #3
0
  public DAOImpl append(String query) {
    String column = ORMConfigBeanUtil.getColumn(clazz, query);
    if (column == null) column = query;

    query = ORMConfigBeanUtil.parseQuery(query, clazz);

    this.condition.append(" ").append(query).append(" ");
    return this;
  }
Example #4
0
 public DAOImpl asc(String fieldName) {
   String column = ORMConfigBeanUtil.getColumn(clazz, fieldName);
   this.orderStr = " ORDER BY " + column + " ASC ";
   this.buffer.put("orderField", column);
   this.buffer.put("orderType", OrderType.ASC_ORDER);
   return this;
 }
Example #5
0
  public DAO setClass(Class<?> clazz) {
    this.clazz = clazz;

    if (Map.class.isAssignableFrom(clazz)) {
      if (this.map != null) {
        selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(map);
        table = (String) map.get("table");
      }
    } else {
      this.table = ORMConfigBeanUtil.getTable(clazz);
      selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(clazz);
    }

    if (selectAllColumn == null || selectAllColumn.trim().length() == 0) selectAllColumn = "*";

    return this;
  }
Example #6
0
  public DAO insert(String... fields) {
    if (clazz == null) return this;

    StringBuilder sb = new StringBuilder();
    String[] columns;
    if (fields == null) columns = ORMConfigBeanUtil.getColumns(clazz);
    else columns = ORMConfigBeanUtil.getColumns(clazz, fields);

    for (String col : columns) {
      if (sb.length() > 0) sb.append(", ");
      sb.append(col);
    }

    this.sql.append(" INSERT INTO ").append(table).append("(").append(sb.toString()).append(") ");

    return this;
  }
  private Sql makeSQL(T t, String condition) {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    String table = ORMConfigBeanUtil.getTable(clazz, false);
    sql.sql = String.format("UPDATE %s %s ;", table, condition);

    return sql;
  }
Example #8
0
  public DAO clear() {
    this.sql = null;
    this.sql = new StringBuilder();
    this.buffer.clear();
    this.buffer.put("orderField", ORMConfigBeanUtil.getIdField(clazz));
    this.buffer.put("orderType", OrderType.DESC_ORDER);
    this.condition = null;
    this.condition = new StringBuilder();
    this.args.clear();
    this.orderStr = "";

    return this;
  }
Example #9
0
  public DAO select(String... fields) {
    if (fields == null || clazz == null) return this;

    StringBuilder sb = new StringBuilder();
    for (String field : fields) {
      if (sb.length() > 0) sb.append(", ");
      String col = ORMConfigBeanUtil.getColumn(clazz, field);
      sb.append(col);
    }
    this.sql.append(" SELECT ").append(sb.toString()).append(" FROM ").append(table).append(" ");

    return this;
  }
  public void init(Object t, List<Field> fields) throws DAOException {
    this.t = t;
    this.fields = fields;
    this.ru = new ReflectUtil(this.t);
    this.table = ORMConfigBeanUtil.getTable(this.t.getClass(), true);
    // 主类的ID属性名
    this.idField = ORMConfigBeanUtil.getIdField(this.t.getClass());
    this.idSetter = ru.getSetter(idField);
    if (this.idSetter == null) throw new DAOException("can not get idSetter.", null);

    this.idGetter = ru.getGetter(idField);
    if (this.idGetter == null) throw new DAOException("can not get idGetter.", null);

    this.idColumn = ORMConfigBeanUtil.getIdColumn(this.t.getClass());

    try {
      Object _idVal = idGetter.invoke(this.t);
      this.idVal = _idVal == null ? null : String.valueOf(_idVal);
    } catch (Exception e) {
      throw new DAOException(idGetter + " invoke exception ", e);
    }
  }
Example #11
0
  private void init(Class<?> clazz, String dsName) {
    if (dsName == null) dsName = DAOConfigConstant.MYDBINFO;

    this.clazz = clazz;
    this.dsName = dsName;
    this.ds = DataSourceWrapCache.get(dsName);

    dbType = DBInfoConfigBeanCache.get(dsName).getDataBaseType();
    if (Map.class.isAssignableFrom(clazz)) {
      if (this.map != null) {
        selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(map);
        table = (String) map.get("table");
      }
    } else {
      this.table = ORMConfigBeanUtil.getTable(clazz);
      selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(clazz);
    }

    if (selectAllColumn == null || selectAllColumn.trim().length() == 0) selectAllColumn = "*";

    this.buffer.put("orderField", ORMConfigBeanUtil.getIdField(clazz));
    this.buffer.put("orderType", OrderType.DESC_ORDER);
  }
Example #12
0
  public DAO values(Object... values) {
    if (values == null) {
      values = new Object[ORMConfigBeanUtil.getColumns(clazz).length];

      for (int i = 0; i < values.length; i++) values[i] = "?";
    }

    StringBuilder sb = new StringBuilder();
    for (Object val : values) {
      if (sb.length() > 0) sb.append(", ");
      sb.append("'").append(val).append("'");
    }
    this.sql.append(" VALUES(").append(sb.toString()).append(") ");

    return this;
  }
Example #13
0
 public <T> Number deleteWhere(Class<T> clazz, String condition, Object[] args)
     throws DAOException {
   Number id = 0;
   if (clazz != null) {
     Connection con = null;
     try {
       con = ds.getConnection();
       String sql =
           SqlFactory.getDeleteSql(new Object[] {clazz.newInstance()})
               .deleteWhere(ORMConfigBeanUtil.parseQuery(condition, clazz));
       id = JdbcUtil.updateWithArgs(con, sql, args);
       // 缓存
     } catch (Exception e) {
       throw new DAOException("", e);
     }
   }
   return id;
 }
Example #14
0
  public DAO set(String[] fields, Object... values) {
    if (fields == null
        || values == null
        || fields.length == 0
        || values.length == 0
        || fields.length != values.length) return this;

    StringBuilder sb = new StringBuilder();
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    for (int i = 0; i < values.length; i++) {
      if (sb.length() > 0) sb.append(", ");
      String col = columns[i];
      Object val = values[i];
      sb.append(col).append(" = '").append(val).append("'");
    }

    this.sql.append(" SET ").append(sb.toString()).append(" ");

    return this;
  }
Example #15
0
  private <T> List<T> query(String sql) {
    List<T> result = null;
    try {

      if (Map.class.isAssignableFrom(this.clazz)) {
        Connection con = ds.getConnection();
        if (args != null && args.size() > 0) {
          result =
              (List<T>) JdbcUtil.getListWithArgs(con, clazz, sql, args.toArray(new Object[] {}));
        } else {
          result = (List<T>) JdbcUtil.getList(con, clazz, sql);
        }

      } else {
        if (args != null && args.size() > 0) {
          result =
              (List<T>)
                  DAOFactory.getSelectDAO(dsName)
                      .selectBySQL(clazz, sql, args.toArray(new Object[] {}));
        } else {
          result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(clazz, sql);
        }
      }

      // this.clear();
      if (result != null && result.size() > 0) {
        for (T t : result) {
          // ToOne relation class cascade select
          final String[] fields = ORMConfigBeanUtil.getToOneField(t.getClass());
          if (fields == null || fields.length == 0) continue;

          DAOFactory.getCascadeDAO(dsName).select(t, fields);
        }
      }

      return result;
    } catch (Exception e) {
      log.error("sql-->" + sql + "exception:" + StringUtil.getExceptionString(e));
      throw new DAOException(sql + " execute exception", e);
    }
  }
  /**
   * 多对多级联删除 1.如果主对象不存在与数据库,不处理 2.否则,检查当前主对象中的关联对象,如果关联对象为空,则删除所有与主对象有关的关联关系。
   * 3.如果当前主对象中含有关联对象,则删除这些关联对象与主对象的关系 4.不会删除主对象
   */
  public void delete() throws DAOException {
    if (this.fields == null || this.fields.size() == 0) return;

    // "delete from {relTable} WHERE {from} = {idVal} ;"
    String format = "delete from %s WHERE %s = ? ";
    if (idVal == null || "0".equals(idVal) || "".equals(idVal)) return;
    else if (DAOFactory.getSelectDAO(dsName).selectOne(t, this.idField) == null) return;

    for (Field f : fields) {
      Method tarGetter = ru.getGetter(f.getName());
      if (tarGetter == null) continue;

      ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class);
      if (ann == null) {
        ann = f.getAnnotation(ManyToMany.class);
        if (ann == null) continue;
      }

      JoinTable join = tarGetter.getAnnotation(JoinTable.class);
      if (join == null) {
        join = f.getAnnotation(JoinTable.class);
        if (join == null) continue;
      }

      JoinColumn[] froms = join.joinColumns();
      if (froms == null || froms.length == 0) continue;

      JoinColumn[] tos = join.inverseJoinColumns();
      if (tos == null || tos.length == 0) continue;

      String relTable = join.name();
      String from = froms[0].name();

      List<?> tarList = null;

      try {
        tarList = (List<?>) tarGetter.invoke(t);
      } catch (Exception e) {
        throw new DAOException(tarGetter + " invoke exception ", e);
      }

      if (tarList == null || tarList.size() == 0) {
        String sql = String.format(format, relTable, from);
        // 删除所有关联记录
        DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(sql, idVal);
      } else {
        // 删除指定关联的记录
        String to = tos[0].name();
        Class<?> tarClass = ann.targetEntity();
        if (void.class.isAssignableFrom(tarClass)) {
          tarClass = ClassUtil.getGenericType(f);
        }

        // "delete from {relTable} where {from} = {idVal} and to = {toVal}"
        String _format = "delete from %s where %s = ? and %s = ?";
        for (int i = 0; i < tarList.size(); i++) {
          Object tarObj = tarList.get(i);
          if (tarObj == null) continue;
          ReflectUtil tarRu = new ReflectUtil(tarObj);
          String tarIdField = ORMConfigBeanUtil.getIdField(tarClass);
          Method tarIdGetter = tarRu.getGetter(tarIdField);
          Object toValObj = null;

          try {
            toValObj = tarIdGetter.invoke(tarObj);
          } catch (Exception e) {
            throw new DAOException(tarIdGetter + "invoke exception ", e);
          }

          if (toValObj == null) continue;

          String toVal = String.valueOf(toValObj);
          if (DAOFactory.getSelectDAO(dsName)
                  .selectOne(tarClass, new String[] {tarIdField}, new String[] {toVal})
              == null) continue;

          String _sql = String.format(_format, relTable, from, to);
          DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(_sql, idVal, toVal);
        }
      }
    }
  }
  private Sql makeSQL(T t) throws SqlCreateException {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    String table;
    String[] columns;
    String[] fields;
    Object[] values = null;
    String idColumn;
    String idField;
    Object idValue = null;
    HashMap<String, Object> map = null;
    if (Map.class.isAssignableFrom(clazz)) {
      map = (HashMap) t;
      table = (String) map.get("table");
      idColumn = (String) map.get("idColumn");
      idField = idColumn;
      idValue = map.get("idValue");
      columns = (String[]) map.get("columns");
      fields = columns;
      values = (Object[]) map.get("values");
    } else {
      table = ORMConfigBeanUtil.getTable(clazz, false);
      columns = ORMConfigBeanUtil.getColumns(clazz);
      fields = ORMConfigBeanUtil.getFields(clazz);
      idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
      idField = ORMConfigBeanUtil.getIdField(clazz);
    }

    StringBuilder valuesSb = new StringBuilder();
    ReflectUtil ru = new ReflectUtil(t);
    try {
      if (map == null) {
        Method idGetter = ru.getGetter(idField);
        if (idGetter == null) throw new SqlCreateException("can not find id getter");
        idValue = idGetter.invoke(t);
      }

      for (int i = 0; i < columns.length; i++) {
        String column = columns[i];
        String field = fields[i];
        Object value = null;
        // id 字段不允许
        if (idColumn != null && idColumn.equalsIgnoreCase(column)) continue;

        if (map != null && values != null) {
          value = values[i];
        } else {
          Method getter = ru.getGetter(field);
          if (getter == null) continue;

          Object _value = getter.invoke(t);
          if (_value == null) continue;

          if (ClassUtil.isPojo(_value.getClass())) {
            Field f = ru.getField(field);
            OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
            if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class);

            ManyToOne manyToOneAnn = null;
            if (oneAnn == null) {
              manyToOneAnn = getter.getAnnotation(ManyToOne.class);
              if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class);
            }

            if (oneAnn != null || manyToOneAnn != null) {
              JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
              if (joinColAnn == null) joinColAnn = f.getAnnotation(JoinColumn.class);

              if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                String refCol = joinColAnn.referencedColumnName();
                String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                ReflectUtil tarRu = new ReflectUtil(_value);
                Method tarFKGetter = tarRu.getGetter(refField);
                value = tarFKGetter.invoke(_value);
              } else {
                ReflectUtil tarRu = new ReflectUtil(_value);
                String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                if (tarFKField != null) {
                  Method tarFKGetter = tarRu.getGetter(tarFKField);
                  value = tarFKGetter.invoke(_value);
                }
              }
            }
            if (value == null) continue;
          } else value = _value;
        }

        if (valuesSb.length() > 0) valuesSb.append(",");

        //				valuesSb.append(column).append(" = '").append(value).append("'");
        valuesSb.append(column).append(" = ? ");
        sql.args.add(value);
      }
    } catch (Exception e) {
      throw new SqlCreateException("" + e.toString(), e);
    }

    //		String condition = new StringBuilder().append(idColumn).append(" =
    // ").append("'").append(idValue).append("'").toString();
    String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
    sql.args.add(idValue);
    sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, valuesSb, condition);
    return sql;
  }
  private Sql makeSQL(T t, String[] fields) throws SqlCreateException {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    // if fields is empty
    if (fields == null || fields.length == 0) {
      fields = ORMConfigBeanUtil.getFields(clazz);
    }
    String table = ORMConfigBeanUtil.getTable(clazz, false);
    StringBuilder values = new StringBuilder();
    ReflectUtil ru = new ReflectUtil(t);
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    String idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
    String idField = ORMConfigBeanUtil.getIdField(clazz);
    Method idGetter = ru.getGetter(idField);
    if (idGetter == null) throw new SqlCreateException("can not find id getter.");
    Object idValue = null;
    try {
      idValue = idGetter.invoke(t);
    } catch (Exception e) {
      throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e);
    }

    for (int i = 0; i < fields.length; i++) {
      String field = fields[i];
      String column = columns[i];
      Method getter = ru.getGetter(field);
      if (getter == null) continue;

      try {
        Object _value = getter.invoke(t);
        if (_value == null) continue;

        Object value = null;

        if (ClassUtil.isPojo(_value.getClass())) {
          Field f = ru.getField(field);
          OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
          if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class);

          ManyToOne manyToOneAnn = null;
          if (oneAnn == null) {
            manyToOneAnn = getter.getAnnotation(ManyToOne.class);
            if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class);
          }

          if (oneAnn != null || manyToOneAnn != null) {
            JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
            if (joinColAnn == null) joinColAnn = f.getAnnotation(JoinColumn.class);

            if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
              String refCol = joinColAnn.referencedColumnName();
              String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
              ReflectUtil tarRu = new ReflectUtil(_value);
              Method tarFKGetter = tarRu.getGetter(refField);
              value = tarFKGetter.invoke(_value);
            } else {
              ReflectUtil tarRu = new ReflectUtil(_value);
              String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
              if (tarFKField != null) {
                Method tarFKGetter = tarRu.getGetter(tarFKField);
                value = tarFKGetter.invoke(_value);
              }
            }
          }

          if (value == null) continue;

        } else {
          value = _value;
        }

        if (values.length() > 0) values.append(", ");

        //				values.append(column).append(" = '").append(value).append("'");
        values.append(column).append(" = ? ");
        sql.args.add(value);
      } catch (Exception e) {
        throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e);
      }
    }

    //		String condition = new StringBuilder().append(idColumn).append(" =
    // ").append("'").append(idValue).append("'").toString();
    String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
    sql.args.add(idValue);
    sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, values, condition);
    return sql;
  }
Example #19
0
 private String makeSQL(T t, String condition) {
   Class<?> clazz = t.getClass();
   String table = ORMConfigBeanUtil.getTable(clazz);
   return String.format("UPDATE %s %s ;", table, condition);
 }
  /**
   * 多对多级联查询 1.当主对象没有包含任何一个关联对象时,默认查询所有与之关联的对象 2.当主对象中包含了关联对象时(含有其id值),则只查询这些关联的对象
   *
   * @param <T>
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws IllegalArgumentException
   */
  public void select() throws DAOException {
    if (this.fields == null || this.fields.size() == 0) return;
    // select %s from {tarTable} where {referencedColumn} in (select {to} from {relTable} where
    // {from} = {idVal})
    String format = "SELECT %s FROM %s WHERE %s IN (SELECT %s FROM %s WHERE %s = ?) ";
    if (idVal == null || "0".equals(idVal) || "".equals(idVal)) return;

    for (Field f : fields) {
      Method tarGetter = ru.getGetter(f.getName());
      if (tarGetter == null) continue;

      ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class);
      if (ann == null) {
        ann = f.getAnnotation(ManyToMany.class);
        if (ann == null) continue;
      }

      JoinTable join = tarGetter.getAnnotation(JoinTable.class);
      if (join == null) {
        join = f.getAnnotation(JoinTable.class);
        if (join == null) continue;
      }

      JoinColumn[] froms = join.joinColumns();
      if (froms == null || froms.length == 0) continue;

      JoinColumn[] tos = join.inverseJoinColumns();
      if (tos == null || tos.length == 0) continue;

      // 多对多关系目标Class
      Class<?> tarClass = ann.targetEntity();
      if (void.class.isAssignableFrom(tarClass)) {
        tarClass = ClassUtil.getGenericType(f);
      }

      String tarTable = ORMConfigBeanUtil.getTable(tarClass, true);
      // 目标类对应的数据库表Id字段
      String referencedColumn = tos[0].referencedColumnName();
      if (referencedColumn == null || referencedColumn.trim().length() == 0)
        referencedColumn = ORMConfigBeanUtil.getIdColumn(tarClass);

      // 目标类在第三方关系表中的字段名
      String to = tos[0].name();

      // 第三方关系表
      String relTable = join.name();

      // 主类在第三方关系表中的字段名
      String from = froms[0].name();

      try {
        List<?> tarList = null;
        tarList = (List<?>) tarGetter.invoke(t);

        if (tarList != null && tarList.size() > 0) {
          for (int i = 0; i < tarList.size(); i++) {
            Object tarObj = tarList.get(i);
            ReflectUtil tarRu = new ReflectUtil(tarObj);
            String tarIdField = ORMConfigBeanUtil.getIdField(tarClass);
            Method tarIdGetter = tarRu.getGetter(tarIdField);
            Object tarIdValObj = tarIdGetter.invoke(tarObj);
            if (tarIdValObj == null) continue;
            String tarIdVal = String.valueOf(tarIdValObj);
            // 查询 select %s from {tarTable} where {tarIdColumn} = {tarIdVal}
            tarObj =
                DAOFactory.getSelectDAO(dsName)
                    .selectOne(tarClass, new String[] {tarIdField}, new String[] {tarIdVal});
          }
        } else {
          String sql =
              String.format(
                  format,
                  ORMConfigBeanUtil.getSelectAllColumn(tarClass),
                  tarTable,
                  referencedColumn,
                  to,
                  relTable,
                  from);
          // 从数据库中取出与当前主对象id关联的所有目标对象,
          tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, idVal);
        }

        // 并注入到当前主对象的属性中
        Method tarSetter = ru.getSetter(f.getName());

        tarSetter.invoke(t, tarList);
      } catch (Exception e) {
        e.printStackTrace();
        throw new DAOException("", e);
      }
    }
  }
Example #21
0
  private String makeSQL(T t, String[] fields) throws SqlCreateException {
    Class<?> clazz = t.getClass();
    String table = ORMConfigBeanUtil.getTable(clazz);
    StringBuilder condition = new StringBuilder();
    StringBuilder values = new StringBuilder();
    ReflectUtil ru = new ReflectUtil(t);
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    String idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
    String idField = ORMConfigBeanUtil.getIdField(clazz);
    Method idGetter = ru.getGetter(idField);
    if (idGetter == null) throw new SqlCreateException("can not find id getter.");

    try {
      condition.append(idColumn).append(" = '").append(idGetter.invoke(t)).append("'");
    } catch (Exception e) {
      throw new SqlCreateException(idGetter + " invoke exception " + e.toString());
    }

    for (int i = 0; i < fields.length; i++) {
      String field = fields[i];
      String column = columns[i];
      Method getter = ru.getGetter(field);
      if (getter == null) continue;

      try {
        Object _value = getter.invoke(t);
        if (_value == null) continue;

        Object value = null;

        if (ClassUtil.isPojo(_value.getClass())) {
          Field f = ru.getField(field);
          OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
          if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class);

          ManyToOne manyToOneAnn = null;
          if (oneAnn == null) {
            manyToOneAnn = getter.getAnnotation(ManyToOne.class);
            if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class);
          }

          if (oneAnn != null || manyToOneAnn != null) {
            ReflectUtil tarRu = new ReflectUtil(_value);
            String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());

            Method tarFKGetter = tarRu.getGetter(tarFKField);
            value = tarFKGetter.invoke(_value);
          }
        }

        if (value == null) value = _value;

        if (values.length() > 0) values.append(", ");

        values.append(column).append(" = '").append(value).append("'");
      } catch (Exception e) {
        throw new SqlCreateException(idGetter + " invoke exception " + e.toString());
      }
    }

    return String.format("UPDATE %s SET %s WHERE %s ;", table, values, condition);
  }
  /** 多对一级联查询 1.获取当前idVal,然后作为条件查询出其外键值,接着通过其外键值查出主对象数据,注入到当前 */
  public void select() {
    if (this.fields == null || this.fields.size() == 0) return;
    if (idVal == null || "0".equals(idVal) || "".equals(idVal)) {
      return;
    }
    //		else if (DAOFactory.getSelectDAO(dsName).selectOne(t, this.idField) == null) {
    //			// 检查一下当前对象的ID是否存在于数据库
    //			return;
    //		}

    for (Field f : fields) {
      Method tarGetter = ru.getGetter(f.getName());
      if (tarGetter == null) continue;

      String fk = null;
      OneToOne ann = tarGetter.getAnnotation(OneToOne.class);
      if (ann == null) ann = f.getAnnotation(OneToOne.class);

      if (ann == null) {
        ManyToOne moAn = tarGetter.getAnnotation(ManyToOne.class);
        if (moAn == null) moAn = f.getAnnotation(ManyToOne.class);

        if (moAn == null) continue;
      }
      String refCol = null;
      JoinColumn joinCol = f.getAnnotation(JoinColumn.class);
      if (joinCol == null) joinCol = tarGetter.getAnnotation(JoinColumn.class);

      if (joinCol == null) fk = f.getName() + "_id";
      else {
        fk = joinCol.name();
        refCol = joinCol.referencedColumnName();
      }

      Class<?> tarClass = f.getType();

      if (refCol == null || refCol.trim().length() == 0)
        refCol = ORMConfigBeanUtil.getIdColumn(tarClass);

      String refField = ORMConfigBeanUtil.getField(tarClass, refCol);

      try {
        Object _tarObj = tarGetter.invoke(t);
        Object tarObj = null;
        boolean flag = false;
        if (_tarObj != null) {
          Method refFieldGetter = new ReflectUtil(_tarObj).getGetter(refField);
          if (refFieldGetter != null && refFieldGetter.invoke(_tarObj) != null)
            tarObj = DAOFactory.getSelectDAO(dsName).selectOne(_tarObj, refField);
          else flag = true;
        } else flag = true;

        if (flag) {
          // select * from {tarTable} where {referencedColumn} = (select {fk} from {table} where
          // {idColumn} = {idVal})
          String format = "select %s from %s where %s = (select %s from %s where %s = %s )";
          String tarTable = ORMConfigBeanUtil.getTable(tarClass, true);

          String sql =
              String.format(
                  format,
                  ORMConfigBeanUtil.getSelectAllColumn(tarClass),
                  tarTable,
                  refCol,
                  fk,
                  table,
                  idColumn,
                  idVal);
          List<?> tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql);
          if (tarList == null || tarList.size() == 0) continue;

          tarObj = tarList.get(0);
        }

        if (tarObj == null) continue;

        Method tarSetter = ru.getSetter(f.getName());
        tarSetter.invoke(t, tarObj);
      } catch (Exception e) {
        throw new DAOException("", e);
      }
    }
  }
Example #23
0
  public <T> Number deleteById(Class<T> clazz, Number id) throws DAOException {
    if (clazz == null || id == null) return 0;

    String field = ORMConfigBeanUtil.getIdField(clazz);
    return this.deleteByFieldIsValue(clazz, field, String.valueOf(id));
  }
Example #24
0
 public DAOImpl and(String fieldName) {
   String column = ORMConfigBeanUtil.getColumn(clazz, fieldName);
   this.condition.append(" AND ").append(column).append(" ");
   return this;
 }