0

現在のプロジェクトの作業で使用しているカスタム 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);
    }
}
}
4

1 に答える 1

2

春になると、特定の方法で物事を行う必要が生じることはめったにありません。その DAO は、読むのが恐ろしいですが、Spring アプリでも他の場所と同様に機能します。当然、Spring の JDBC サポートが提供する利点は得られません。

于 2012-06-04T04:08:18.977 に答える