我对SpringDAO层支持的总结
之前发过两篇关于Spring的总结帖子 反响还不错,再把剩下的几篇发上来。共享给大家。
?
我对IoC/DI的理解我对AOP的理解
?
?
public interface JdbcOperations { //接口定义行为集 public Object execute() throws SQLException ;}?
public abstract class AbstractJdbcOperations implements JdbcOperations { @Override public final Object execute() throws SQLException { Connection conn = DataSourceUtils.getConnection(); try { Object retVal = doInConnection(conn); conn.commit(); return retVal; }catch (Exception e) { conn.rollback(); throw e;} finally { conn.close(); } } public abstract Object doInConnection(Connection conn) throws SQLException;}?
public class DataSourceUtils { public static Connection getConnection() { //返回数据库连接 return null; }}JdbcOperations select = new AbstractJdbcOperations() { @Override public Object doInConnection(Connection conn) throws SQLException { PreparedStatement pstmt = conn.prepareStatement("select * from tbl_user"); ResultSet rs = pstmt.executeQuery(); List result = null; //处理结果集 return result; } }; select.execute();?public interface JdbcOprations2 { //接口定义行为集 public Object execute(ConnectionCallback callback) throws Exception ;}
public interface ConnectionCallback { public Object doInConnection(Connection conn) throws SQLException;}?
public class JdbcTemplate implements JdbcOprations2 { @Override public Object execute(ConnectionCallback callback) throws Exception { Connection conn = DataSourceUtils.getConnection(); try { Object retVal = callback.doInConnection(conn); conn.commit(); return retVal; } catch (Exception e) { conn.rollback(); throw e; } finally { conn.close(); } } }?
jdbcTemplate.execute(new ConnectionCallback() { @Override public Object doInConnection(Connection conn) throws SQLException { //可变操作 return null; } });?
public interface PreparedStatementCallback { public Object doInPreparedStatement(PreparedStatement pstmt) throws SQLException;}?缺点:灵活但不通用?????
?
?


