当前位置:Gxlcms > 数据库问题 > 《Spring技术内幕》笔记-第五章 数据库操作组件的实现

《Spring技术内幕》笔记-第五章 数据库操作组件的实现

时间:2021-07-01 10:21:17 帮助过:3人阅读

  • public void execute(final String sql) throws DataAccessException {
  • if (logger.isDebugEnabled()) {
  • logger.debug("Executing SQL statement [" + sql + "]");
  • }
  • class ExecuteStatementCallback implements StatementCallback<Object>, SqlProvider {
  • @Override
  • public Object doInStatement(Statement stmt) throws SQLException {//重写
  • stmt.execute(sql);
  • return null;
  • }
  • @Override
  • public String getSql() {
  • return sql;
  • }
  • }
  • execute(new ExecuteStatementCallback());
  • }
    1. @Override
    2. public <T> T execute(StatementCallback<T> action) throws DataAccessException {
    3. Assert.notNull(action, "Callback object must not be null");
    4. Connection con = DataSourceUtils.getConnection(getDataSource());//获取COnnection
    5. Statement stmt = null;
    6. try {
    7. Connection conToUse = con;
    8. if (this.nativeJdbcExtractor != null &&
    9. this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
    10. conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
    11. }
    12. stmt = conToUse.createStatement();//Statement
    13. applyStatementSettings(stmt);
    14. Statement stmtToUse = stmt;
    15. if (this.nativeJdbcExtractor != null) {
    16. stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
    17. }
    18. T result = action.doInStatement(stmtToUse);//调用传入接口的子类的实现
    19. handleWarnings(stmt);
    20. return result;
    21. }
    22. catch (SQLException ex) {
    23. // Release Connection early, to avoid potential connection pool deadlock
    24. // in the case when the exception translator hasn‘t been initialized yet.
    25. JdbcUtils.closeStatement(stmt);
    26. stmt = null;
    27. DataSourceUtils.releaseConnection(con, getDataSource());
    28. con = null;
    29. throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);
    30. }
    31. finally {
    32. JdbcUtils.closeStatement(stmt);
    33. DataSourceUtils.releaseConnection(con, getDataSource());
    34. }
    35. }

    4,JdbcTemplate的query方法

    1. @Override
    2. public <T> T query(final String sql, final ResultSetExtractor<T> rse) throws DataAccessException {
    3. Assert.notNull(sql, "SQL must not be null");
    4. Assert.notNull(rse, "ResultSetExtractor must not be null");
    5. if (logger.isDebugEnabled()) {
    6. logger.debug("Executing SQL query [" + sql + "]");
    7. }
    8. class QueryStatementCallback implements StatementCallback<T>, SqlProvider {
    9. @Override
    10. public T doInStatement(Statement stmt) throws SQLException {//内部类重写此方法
    11. ResultSet rs = null;
    12. try {
    13. rs = stmt.executeQuery(sql);
    14. ResultSet rsToUse = rs;
    15. if (nativeJdbcExtractor != null) {
    16. rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
    17. }
    18. return rse.extractData(rsToUse);
    19. }
    20. finally {
    21. JdbcUtils.closeResultSet(rs);
    22. }
    23. }
    24. @Override
    25. public String getSql() {
    26. return sql;
    27. }
    28. }
    29. return execute(new QueryStatementCallback());
    30. }

    我们查看在execute方法中的,发现最终调用的是doInStatement,该方法均在内部类中重写。

    5,使用Connection。

        ?Spring通过DataSourceUtils对Connection进行管理。在数据库应用中,数据库的Connection的使用往往和事务处理相关。


    1. public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
    2. try {
    3. return doGetConnection(dataSource);
    4. }
    5. catch (SQLException ex) {
    6. throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
    7. }
    8. }

    1. public static Connection doGetConnection(DataSource dataSource) throws SQLException {
    2. Assert

    人气教程排行