我有一个自定义 DAO 生成器,我在当前项目的工作中使用它。对于下一个项目,我正在考虑使用 Spring 3.1,因为这些特性。我的问题是:¿我的应用程序生成的 DAO 在 Spring 环境中仍然可以正常工作吗?或者我必须使用更改它来使用 JdbcTemplates。我粘贴了一个 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);
}
}
}