Exemple #1
1
  @Override
  public Map<String, Object> saveMap(Map<String, Object> object, Class clazz) throws Exception {

    StringBuilder sql = new StringBuilder();
    EntityInfo entityInfo = ClassUtils.getEntityInfoByClazz(clazz);

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

    List<String> columns = new ArrayList<String>();
    List<Object> values = new ArrayList<Object>();
    Map<String, String> ptcMap = ClassUtils.propToColumnMap.get(entityInfo.getClazzName());
    for (Map.Entry<String, Object> entry : object.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) {
        object.put(entityInfo.getPkName(), id);
      }
    } else {
      add(sql.toString(), values);
    }
    return object;
  }
Exemple #2
0
  public SqlHelper getUpdateSql(Map<String, Object> object, EntityInfo entityInfo) {

    Object id = object.get(entityInfo.getPkName());

    if (id == null) {
      throw new DaoException("缺少主键id参数");
    }

    object.remove(entityInfo.getPkName());

    SqlHelper sqlHelper = new SqlHelper("UPDATE ");
    sqlHelper.append(entityInfo.getTableName()).append(" SET ");
    //        List<String> columns = new ArrayList<String>();
    List<Object> values = new ArrayList<Object>();
    Map<String, String> ptcMap = ClassUtils.propToColumnMap.get(entityInfo.getClazzName());
    StringBuilder columnSql = new StringBuilder();
    for (Map.Entry<String, Object> entry : object.entrySet()) {
      if (entry.getValue() != null) {
        values.add(entry.getValue());
        if (!"".equals(columnSql.toString())) {
          columnSql.append(",");
        }
        columnSql.append(ptcMap.get(entry.getKey()));
        columnSql.append("=");
        columnSql.append("?");
      }
    }
    if (values.size() < 1) {
      throw new DaoException("没有要更新的参数");
    }

    sqlHelper.append(columnSql.toString());
    sqlHelper.append(" WHERE ");
    sqlHelper.append(entityInfo.getPkClumnName());
    sqlHelper.append("=?");
    return sqlHelper;
  }