Beispiel #1
0
  /**
   * 插入vo的所有字段.如果vo的id有值并且值大于0,则使用此值插入,否则生成一个id插入.
   * 此方法不依赖于数据库id的自增,使用max(id)+1的方式生成id.所以此方法会开启一个事务.如果失败,会放弃添加并回滚.
   *
   * @param t vo
   */
  public void insert(T t) throws Exception {
    String sql = this.generator.insert(t.getClass());
    Connection con = tran.getConnection(jndiName);

    int id = this.getId(t);
    if (id <= 0) {
      String next = this.generator.nextId(t.getClass());
      ResultSet rs = con.createStatement().executeQuery(next);
      if (rs.next()) {
        Object o = rs.getObject(1);
        if (o == null) { // 可能还没有记录
          id = 1;
        } else {
          id = Integer.valueOf(String.valueOf(o));
        }
      }
    }
    if (id <= 0) {
      return;
    }
    PreparedStatement ps = con.prepareStatement(sql);
    Field[] fields = listFieldsWithoutId(t.getClass());
    int index = 1;
    ps.setInt(index++, id);
    for (Field f : fields) {
      this.setParamter(ps, index++, t, f);
    }
    ps.executeUpdate();
  }
Beispiel #2
0
 /**
  * 更新vo,使用vo的id作为条件,更新vo的所有字段.<br>
  * 慎用此方法,有可能更新到你不愿意更新的字段
  *
  * @param t vo
  */
 public void update(T t) throws Exception {
   String sql = this.generator.update(t.getClass());
   Connection con = tran.getConnection(jndiName);
   PreparedStatement ps = con.prepareStatement(sql);
   int index = 1;
   Field[] fields = listFieldsWithoutId(t.getClass());
   for (Field f : fields) {
     this.setParamter(ps, index++, t, f);
   }
   ps.setInt(index, this.getId(t));
   ps.executeUpdate();
 }
Beispiel #3
0
 /**
  * 获取一个vo
  *
  * @param id vo的id
  * @return vo或者null
  */
 public T fetch(Object id) throws Exception {
   Class<T> type = this.getPoType();
   String sql = this.generator.fetch(type);
   Connection con = tran.getConnection(jndiName);
   PreparedStatement ps = con.prepareStatement(sql);
   ps.setObject(1, id);
   ResultSet rs = ps.executeQuery();
   if (rs.next()) {
     T t = type.cast(type.newInstance());
     this.fillVO(rs, t);
     return t;
   }
   return null;
 }
Beispiel #4
0
  /**
   * 删除一个vo
   *
   * @param o vo或者id
   */
  public void delete(Object o) throws Exception {
    if (o == null) {
      return;
    }
    Connection con = tran.getConnection(jndiName);
    int id = this.getVOId(o);
    if (id > 0) {
      String sql = this.generator.delete(this.getPoType());

      PreparedStatement ps = con.prepareStatement(sql);
      ps.setObject(1, id);
      ps.executeUpdate();
    } else {
      new RuntimeException("传入的参数:" + o + ",无法正确得到id!");
    }
  }
Beispiel #5
0
 /**
  * 查询所有的vo,没有考虑分页
  *
  * @return vo的list,没有任何可用值返回emptylist
  */
 public List<T> query(Cnd cnd) throws Exception {
   Class<T> type = this.getPoType();
   String sql = this.generator.query(type);
   if (cnd != null) {
     sql += cnd.toString();
   }
   Connection con = tran.getConnection(jndiName);
   PreparedStatement ps = con.prepareStatement(sql);
   ResultSet rs = ps.executeQuery();
   ArrayList<T> list = new ArrayList<T>();
   while (rs.next()) {
     T t = type.cast(type.newInstance());
     this.fillVO(rs, t);
     list.add(t);
   }
   return list;
 }