예제 #1
0
  @Override
  public int update(Object object) throws Exception {

    EntityInfo entityInfo = ClassUtils.getEntityInfoByObject(object);
    Map<String, Object> valueMap = ClassUtils.describe(object);

    SqlHelper sqlHelper = getUpdateSql(valueMap, entityInfo);

    return update(sqlHelper);
  }
예제 #2
0
  @Override
  public Object save(Object object) throws Exception {

    StringBuilder sql = new StringBuilder();
    EntityInfo entityInfo = ClassUtils.getEntityInfoByObject(object);

    sql.append("INSERT INTO ");
    sql.append(entityInfo.getTableName());
    sql.append("(");

    Map<String, Object> valueMap = ClassUtils.describe(object);
    List<String> columns = new ArrayList<String>();
    List<Object> values = new ArrayList<Object>();
    Map<String, String> ptcMap = ClassUtils.propToColumnMap.get(entityInfo.getClazzName());

    if (entityInfo.getStrategy().equals(GenerationType.IDENTITY)) {
      valueMap.remove(entityInfo.getPkName());
    }

    for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
      columns.add(ptcMap.get(entry.getKey()));
      values.add(entry.getValue());
    }
    sql.append(StringUtils.join(columns, ","));
    sql.append(") VALUES(");
    String[] params = new String[values.size()];
    Arrays.fill(params, "?");
    sql.append(StringUtils.join(params, ","));
    sql.append(")");
    if (entityInfo.getStrategy().equals(GenerationType.IDENTITY)) {
      Long id = addReutrnId(sql.toString(), values);
      if (id != null) {
        ClassUtils.setFieldValue(object, entityInfo.getPkName(), id);
      }
    } else {
      add(sql.toString(), values);
    }
    return object;
  }