Ejemplo n.º 1
0
 /**
  * 根据表名和id从数据库中载入一条记录,装配为实体对象返回。<br>
  * 如果该表未配置映射类,则默认映射到java.util.Map;<br>
  * 如果未配置列名与属性名的映射规则,则默认采用同名映射;<br>
  * 注意:表的标识字段由用户预先配置,也可动态修改。
  *
  * @param tablename 表名
  * @param id 标识字段的值
  * @return 实体对象(POJO或Map)
  * @throws E5Exception
  */
 public static Object get(String tableName, Object id) throws E5Exception {
   DBSession ss = null;
   try {
     ss = Context.getDBSession();
     return ss.load(tableName, id);
   } catch (LaterDataTransferException e) {
     throw new E5Exception(e);
   } catch (SQLException e) {
     throw new E5Exception(e);
   } finally {
     CloseHelper.closeQuietly(ss);
   }
 }
Ejemplo n.º 2
0
  /**
   * 根据给定查询条件查询指定表,返回实体对象列表。<br>
   * 如果该表未配置映射类,则默认映射到java.util.Map;<br>
   * 如果未配置列名与属性名的映射规则,则默认采用同名映射
   *
   * @param queryParams 查询参数 {列名 - 列值} 注意:不能含null值
   * @param tablename 表名
   * @return bean的集合(若此表存在mappingClass,则为该类实例;否则为Map实例)
   * @throws E5Exception
   */
  public static List query(Map queryParams, String tableName) throws E5Exception {
    DBSession ss = null;
    try {
      ss = Context.getDBSession();
      return ss.query(tableName, queryParams);

    } catch (LaterDataTransferException e) {
      throw new E5Exception(e);
    } catch (SQLException e) {
      throw new E5Exception(e);
    } finally {
      CloseHelper.closeQuietly(ss);
    }
  }
Ejemplo n.º 3
0
 /**
  * 使用DBSession而不是BaseDAO对一个数据库对象进行创建。 要求提前已经用DBContext进行了属性和数据库表字段的映射。
  *
  * @param obj 实体bean对象
  * @param tableName 表名
  * @return 成功则返回1
  * @throws E5Exception
  */
 public static int create(Object obj, String tableName) throws E5Exception {
   DBSession ss = null;
   try {
     ss = Context.getDBSession();
     ss.store(tableName, obj);
     return 1;
   } catch (LaterDataTransferException e) {
     throw new E5Exception(e);
   } catch (SQLException e) {
     throw new E5Exception(e);
   } finally {
     CloseHelper.closeQuietly(ss);
   }
 }
Ejemplo n.º 4
0
 /**
  * 更新数据库中给定的那些bean属性。 使用DBSession而不是BaseDAO对一个数据库对象进行修改。 要求提前已经用DBContext进行了属性和数据库表字段的映射。
  *
  * @param bean
  * @param tableName 表名
  * @param dirtyFields 以逗号分隔的bean属性列表
  * @throws E5Exception
  */
 public static int update(Object bean, String tableName, String dirtyFields) throws E5Exception {
   String[] dirty = dirtyFields.split(",");
   DBSession ss = null;
   try {
     ss = Context.getDBSession();
     ss.update(tableName, bean, dirty);
     return 1;
   } catch (LaterDataTransferException e) {
     throw new E5Exception(e);
   } catch (SQLException e) {
     throw new E5Exception(e);
   } finally {
     CloseHelper.closeQuietly(ss);
   }
 }
Ejemplo n.º 5
0
  /**
   * 使用DBSession而不是BaseDAO对一个数据库对象进行删除。 要求提前已经用DBContext进行了属性和数据库表字段的映射。
   *
   * @param queryParams 数据库表字段的名值对
   * @param tableName 表名
   * @return 删除成功的个数
   * @throws E5Exception
   */
  public static int delete(Map queryParams, String tableName) throws E5Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("delete from ").append(tableName).append(" where ");

    for (Iterator i = queryParams.entrySet().iterator(); i.hasNext(); ) {
      Map.Entry me = (Map.Entry) i.next();
      sb.append(me.getKey()).append("=?");

      if (i.hasNext()) sb.append(" and ");
    }

    DBSession ss = null;
    try {
      ss = Context.getDBSession();
      return ss.executeUpdate(sb.toString(), queryParams.values().toArray());
    } catch (LaterDataTransferException e) {
      throw new E5Exception(e);
    } catch (SQLException e) {
      throw new E5Exception(e);
    } finally {
      CloseHelper.closeQuietly(ss);
    }
  }