2012-06-04 58 views
0

我有一個自定義的DAO生成器,用於當前項目的工作中。對於下一個項目,我在考慮使用Spring 3.1,因爲這些功能。我的問題是:使用我的應用程序生成的DAO在Spring環境中仍能正常工作嗎?或者我必須使用將其更改爲使用JdbcTemplates。我粘貼一個DAO的樣本。帶有彈簧的舊DAO

public class ProductsDAOImpl implements ProductsDAO { 

private static Log log = LogFactory.getLog(ProductsDAOImpl.class); 
private Exception error; 
private String msg; 
private JdbcTransaction jdbcTransaction; 

@Override 
public void setJdbcTransaction(JdbcTransaction trans) { 
    this.jdbcTransaction = trans; 
} 

@Override 
public void createProduct(Products dto) throws DAOException { 
    if (getProduct(dto.getProductID()) != null) { 
     throw new DAOException("El id " + dto.getProductID() + " ya está siendo  usado"); 
    } 
    Connection conn = getConnection(); 
    PreparedStatement ps = null; 
    error = null; 
    msg = ""; 
    int i = 1; 
    try { 
     String sql = "insert into products (ProductID, ProductName, SupplierID, " 
       + "CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, " 
       + "ReorderLevel, Discontinued) values (?, ?, ?, " 
       + "?, ?, ?, ?, ?," 
       + "?, ?)"; 
     log.info("ProductsDAOImpl.createProduct, id = " + dto.getProductID()); 
     ps = conn.prepareStatement(sql); 
     ps.setInt(i++, dto.getProductID()); 
     ps.setString(i++, dto.getProductName()); 
     ps.setInt(i++, dto.getSupplierID()); 
     ps.setInt(i++, dto.getCategoryID()); 
     ps.setString(i++, dto.getQuantityPerUnit()); 
     ps.setDouble(i++, dto.getUnitPrice()); 
     ps.setInt(i++, dto.getUnitsInStock()); 
     ps.setInt(i++, dto.getUnitsOnOrder()); 
     ps.setInt(i++, dto.getReorderLevel()); 
     ps.setShort(i++, dto.getDiscontinued()); 
     ps.executeUpdate(); 
    } catch (SQLException e) { 
     error = e; 
     msg = "Create failed " + e.getMessage(); 
    } finally { 
     close(conn, ps, null); 
     checkOk(); 
    } 

} 

@Override 
public void updateProduct(Products dto) throws DAOException { 
    if(getProduct(dto.getProductID())==null) { 
     throw new DAOException("Product with ID " + dto.getProductID() + "does not exist"); 
    } 
    Connection conn = getConnection(); 
    PreparedStatement ps = null; 
    error = null; 
    msg = ""; 
    int i = 1; 
    try { 
     log.info("ProductsDAOImpl.updateProduct, id" + dto.getProductID()); 
     String sql = "update products set ProductName = ?, SupplierID = ?, " 
       + "CategoryID = ?, QuantityPerUnit = ?, UnitPrice = ?, " 
       + "UnitsInStock = ?, UnitsOnOrder = ?, " 
       + "ReorderLevel = ?, Discontinued = ? where ProductID = ?"; 
     ps = conn.prepareStatement(sql); 
     ps.setString(i++, dto.getProductName()); 
     ps.setInt(i++, dto.getSupplierID()); 
     ps.setInt(i++, dto.getCategoryID()); 
     ps.setString(i++, dto.getQuantityPerUnit()); 
     ps.setDouble(i++, dto.getUnitPrice()); 
     ps.setInt(i++, dto.getUnitsInStock()); 
     ps.setInt(i++, dto.getUnitsOnOrder()); 
     ps.setInt(i++, dto.getReorderLevel()); 
     ps.setShort(i++, dto.getDiscontinued()); 
     ps.setInt(i++, dto.getProductID()); 
     ps.executeUpdate(); 
    } catch (SQLException e) { 
     error = e; 
     msg = "Update failed " + e.getMessage(); 
    } finally { 
     close(conn, ps, null); 
     checkOk(); 
    } 
} 

@Override 
public void deleteProduct(int productID) throws DAOException { 
    if(getProduct(productID)==null) { 
     throw new DAOException("Id " + productID +" not found"); 
    } 
    Connection conn = getConnection(); 
    PreparedStatement ps = null; 
    error = null; 
    msg = ""; 
    try { 
     log.info("ProductsDAOImpl.deleteProduct, id = " + productID); 
     String sql = "delete from products where productid = ?"; 
     ps = conn.prepareStatement(sql); 
     ps.setInt(1, productID); 
     ps.executeUpdate(); 
    } catch (SQLException e) { 
     error = e; 
     msg = "Delete failed " + e.getMessage(); 
    } finally { 
     close(conn, ps, null); 
     checkOk(); 
    } 
} 

@Override 
public List<Products> getAll() throws DAOException { 
    List<Products> list = new ArrayList<Products>(); 
    Connection conn = getConnection(); 
    Statement s = null; 
    ResultSet rs = null; 
    try { 
     log.info("ProductsDAOImpl.getAll"); 
     s = conn.createStatement(); 
     rs = s.executeQuery("select * from products"); 
     list = makeProductsObjectsFromResultSet(rs); 
    } catch (SQLException e) { 
     error = e; 
     msg = "getAll failed " + e.getMessage(); 
    } finally { 
     close(conn, s, rs); 
     checkOk(); 
    } 
    return list; 

} 

@Override 
public List<Products> selectByQuery(String sql) throws DAOException { 
    List<Products> list = new ArrayList<Products>(); 
    Connection conn = getConnection(); 
    Statement s = null; 
    ResultSet rs = null; 

    try { 
     log.info("ProductsDAO.selectByQuery"); 
     s = conn.createStatement(); 
     rs = s.executeQuery(sql); 
     list = makeProductsObjectsFromResultSet(rs); 
    } catch (SQLException e) { 
     error = e; 
     msg = "selectByQuery failed " + e.getMessage(); 
    } finally { 
     close(conn, s, rs); 
     checkOk(); 
    } 
    return list; 
} 

@Override 
public Products getProduct(int productID) throws DAOException { 
    Products prod = null; 
    Connection conn = getConnection(); 
    PreparedStatement ps = null; 
    ResultSet rs = null; 
    try { 
     log.info("Products.getProduct, id = " + productID); 
     ps = conn.prepareStatement("select * from products where productid = ?"); 
     ps.setInt(1, productID); 
     rs = ps.executeQuery(); 
     List<Products> list = makeProductsObjectsFromResultSet(rs); 
     if (!list.isEmpty()) { 
      prod = list.get(0); 
     } 
    } catch (SQLException e) { 
     error = e; 
     msg = "getProduct failed " + e.getMessage(); 
    } finally { 
     close(conn, ps, rs); 
    } 
    return prod; 
} 

public List<Products> makeProductsObjectsFromResultSet(ResultSet rs) throws SQLException { 
    List<Products> list = new ArrayList<Products>(); 
    while (rs.next()) { 
     Products dto = new Products(); 
     dto.setCategoryID(rs.getInt("categoryID")); 
     dto.setDiscontinued(rs.getShort("discontinued")); 
     dto.setProductID(rs.getInt("productID")); 
     dto.setProductName(rs.getString("productName")); 
     dto.setQuantityPerUnit(rs.getString("quantityPerUnit")); 
     dto.setReorderLevel(rs.getInt("reorderLevel")); 
     dto.setSupplierID(rs.getInt("supplierID")); 
     dto.setUnitPrice(rs.getDouble("unitPrice")); 
     dto.setUnitsInStock(rs.getInt("unitsInStock")); 
     dto.setUnitsOnOrder(rs.getInt("unitsOnOrder")); 
     list.add(dto); 
    } 
    return list; 
} 

private Connection getConnection() throws DAOException { 
    Connection conn = null; 
    if (jdbcTransaction == null) { 
     conn = ResourceManager.getConnection(); 
    } else { 
     conn = jdbcTransaction.getConnection(); 
    } 
    return conn; 
} 

private void close(Connection conn, Statement s, ResultSet rs) { 
    closeResultSet(rs); 
    closeStatement(s); 
    closeConnection(conn); 
} 

public void closeResultSet(ResultSet rs) { 
    if (rs != null) { 
     try { 
      rs.close(); 
     } catch (SQLException e) { 
      if (error == null) { 
       error = e; 
       msg = "Close result failed"; 
      } 
     } 
    } 
} 

private void closeConnection(Connection conn) { 
    if (conn != null) { 
     try { 
      if (conn.getAutoCommit()) { 
       conn.close(); 
       conn = null; 
      } 
     } catch (SQLException e) { 
      if (error == null) { 
       error = e; 
       msg = "Close conn failed"; 
      } 
     } 
    } 
} 

private void closeStatement(Statement s) { 
    if (s != null) { 
     try { 
      s.close(); 
     } catch (SQLException e) { 
      if (error == null) { 
       error = e; 
       msg = "Close statement failed"; 
      } 
     } 
    } 
} 

private void checkOk() throws DAOException { 
    if (error != null) { 
     throw new DAOException(msg, error); 
    } 
} 
} 

回答

2

春天很少,如果有的話,需要你做的事情一定的方式。這個DAO雖然可怕地閱讀,但在Spring應用程序中也可以像其他任何地方一樣工作。當然,你不會得到Spring的JDBC支持提供的任何優勢。

+0

是的,我需要做一些清理我的代碼。感謝您的誠實和您的建議。 –

+0

你能提一下Spring的JDBC提供的一些優點嗎? –

+0

我剛剛回答[另一個問題就是這樣](http://stackoverflow.com/questions/10858251/how-to-integrate-spring-with-jdbc/10858279#10858279)。 –