예제 #1
0
  /**
   * 插入一条记录
   *
   * @param sql
   * @param params
   * @return
   */
  public static int insert(String sql, Object... params) {
    StringBuffer sqlBuf = new StringBuffer(sql);
    sqlBuf.append(" values (");

    int start = sql.indexOf("(") + 1;
    int end = sql.indexOf(")");
    String a = sql.substring(start, end);
    String[] fields = a.split(",");

    Map<String, Object> map = new HashMap<String, Object>();

    int i = 0;
    for (String name : fields) {
      sqlBuf.append(":" + name.trim() + " ,");
      map.put(name.trim(), params[i]);
      i++;
    }

    String newSql = sqlBuf.substring(0, sqlBuf.length() - 1) + ")";

    Connection con = sql2o.open();
    Query query = con.createQuery(newSql);

    executeQuery(query, map);

    int res = query.executeUpdate().getResult();

    con.close();

    return res;
  }
예제 #2
0
 public static <T> List<T> getList(String sql, Class<T> clazz, Map<String, Object> params) {
   Connection con = sql2o.open();
   Query query = con.createQuery(sql);
   executeQuery(query, params);
   List<T> list = query.executeAndFetch(clazz);
   con.close();
   return list;
 }
예제 #3
0
 public static List<Map<String, Object>> getMapList(String sql, Map<String, Object> params) {
   Connection con = sql2o.open();
   Query query = con.createQuery(sql);
   executeQuery(query, params);
   List<Map<String, Object>> t = query.executeAndFetchTable().asList();
   con.close();
   return t;
 }
예제 #4
0
 /**
  * 带参数更新
  *
  * @param sql
  * @param params
  * @return
  */
 public static int update(String sql, Map<String, Object> params) {
   Connection con = sql2o.open();
   Query query = con.createQuery(sql);
   executeQuery(query, params);
   int res = query.executeUpdate().getResult();
   con.close();
   return res;
 }
예제 #5
0
 @SuppressWarnings("unchecked")
 public static Map<String, Object> getMap(String sql, Map<String, Object> params) {
   Connection con = sql2o.open();
   Query query = con.createQuery(sql);
   executeQuery(query, params);
   Map<String, Object> t = (Map<String, Object>) query.executeScalar();
   con.close();
   return t;
 }