我有一个自定义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);
}
}
}
答案 0 :(得分:2)
春天很少,如果有的话,要求你以某种方式做事。 DAO虽然可怕阅读,但在Spring应用程序中也可以像在其他任何地方一样工作。当然,您不会获得Spring的JDBC支持提供的任何优势。