深入解析Spring架構(gòu)與設(shè)計原理-數(shù)據(jù)庫的操作實現(xiàn)

關(guān)于Spring JDBC
還是從Spring JDBC說起吧,雖然現(xiàn)在應(yīng)用很多都是直接使用Hibernate或者其他的ORM工具。但JDBC畢竟還是很基本的,其中的JdbcTemplate就是我們經(jīng)常使用的,比如JDBCTemplate的execute方法,就是一個基本的方法,在這個方法的實現(xiàn)中,可以看到對數(shù)據(jù)庫操作的基本過程。

創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、中衛(wèi)網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為中衛(wèi)等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

//execute方法執(zhí)行的是輸入的sql語句  
public void execute(final String sql) throws DataAccessException {  
    if (logger.isDebugEnabled()) {  
        logger.debug("Executing SQL statement [" + sql + "]");  
    }  
    class ExecuteStatementCallback implements StatementCallback<Object>, SqlProvider {  
        public Object doInStatement(Statement stmt) throws SQLException {  
            stmt.execute(sql);  
            return null;  
        }  
        public String getSql() {  
            return sql;  
        }  
    }  
    execute(new ExecuteStatementCallback());  
}  
//這是使用java.sql.Statement處理靜態(tài)SQL語句的方法  
public <T> T execute(StatementCallback<T> action) throws DataAccessException {  
    Assert.notNull(action, "Callback object must not be null");  
    //這里取得數(shù)據(jù)庫的Connection,這個數(shù)據(jù)庫的Connection已經(jīng)在Spring的事務(wù)管理之下  
    Connection con = DataSourceUtils.getConnection(getDataSource());  
    Statement stmt = null;  
    try {  
        Connection conToUse = con;  
        if (this.nativeJdbcExtractor != null &&  
                this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {  
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);  
        }  
        //創(chuàng)建Statement  
        stmt = conToUse.createStatement();  
        applyStatementSettings(stmt);  
        Statement stmtToUse = stmt;  
        if (this.nativeJdbcExtractor != null) {  
            stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);  
        }  
        //這里調(diào)用回調(diào)函數(shù)  
        T result = action.doInStatement(stmtToUse);  
        handleWarnings(stmt);  
        return result;  
    }  
    catch (SQLException ex) {  
        // Release Connection early, to avoid potential connection pool deadlock  
        // in the case when the exception translator hasn't been initialized yet.  
        //如果捕捉到數(shù)據(jù)庫異常,把數(shù)據(jù)庫Connection釋放,同時拋出一個經(jīng)過Spring轉(zhuǎn)換過的Spring數(shù)據(jù)庫異常  
        //Spring做了一項有意義的工作,就是把這些數(shù)據(jù)庫異常統(tǒng)一到自己的異常體系里了  
        JdbcUtils.closeStatement(stmt);  
        stmt = null;  
        DataSourceUtils.releaseConnection(con, getDataSource());  
        con = null;  
        throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);  
    }  
    finally {  
        JdbcUtils.closeStatement(stmt);  
        //釋放數(shù)據(jù)庫connection  
        DataSourceUtils.releaseConnection(con, getDataSource());  
    }  
}  

在使用數(shù)據(jù)庫的時候,有一個很重要的地方就是對數(shù)據(jù)庫連接的管理,在這里,是由DataSourceUtils來完成的。Spring通過這個輔助類來對數(shù)據(jù)的Connection進行管理。比如通過它來完成打開和關(guān)閉Connection等操作。DataSourceUtils對這些數(shù)據(jù)庫Connection管理的實現(xiàn), 如以下代碼所示。

//這是取得數(shù)據(jù)庫連接的調(diào)用,實現(xiàn)是通過調(diào)用doGetConnection完成的,這里執(zhí)行了異常的轉(zhuǎn)換操作  
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {  
    try {  
        return doGetConnection(dataSource);  
    }  
    catch (SQLException ex) {  
        throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);  
    }  
}  
public static Connection doGetConnection(DataSource dataSource) throws SQLException {  
    Assert.notNull(dataSource, "No DataSource specified");  
    //把對數(shù)據(jù)庫的Connection放到事務(wù)管理中進行管理,這里使用TransactionSynchronizationManager中定義的ThreadLocal變量來和線程綁定數(shù)據(jù)庫連接  
    //如果在TransactionSynchronizationManager中已經(jīng)有與當(dāng)前線程綁定數(shù)據(jù)庫連接,那就直接取出來使用  
    ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);  
    if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {  
        conHolder.requested();  
        if (!conHolder.hasConnection()) {  
            logger.debug("Fetching resumed JDBC Connection from DataSource");  
            conHolder.setConnection(dataSource.getConnection());  
        }  
        return conHolder.getConnection();  
    }  
    // Else we either got no holder or an empty thread-bound holder here.  
    // 這里得到需要的數(shù)據(jù)庫Connection,在Bean配置文件中定義好的,  
    // 同時最后把新打開的數(shù)據(jù)庫Connection通過TransactionSynchronizationManager和當(dāng)前線程綁定起來。  
    logger.debug("Fetching JDBC Connection from DataSource");  
    Connection con = dataSource.getConnection();  
  
    if (TransactionSynchronizationManager.isSynchronizationActive()) {  
        logger.debug("Registering transaction synchronization for JDBC Connection");  
        // Use same Connection for further JDBC actions within the transaction.  
        // Thread-bound object will get removed by synchronization at transaction completion.  
        ConnectionHolder holderToUse = conHolder;  
        if (holderToUse == null) {  
            holderToUse = new ConnectionHolder(con);  
        }  
        else {  
            holderToUse.setConnection(con);  
        }  
        holderToUse.requested();  
        TransactionSynchronizationManager.registerSynchronization(  
                new ConnectionSynchronization(holderToUse, dataSource));  
        holderToUse.setSynchronizedWithTransaction(true);  
        if (holderToUse != conHolder) {  
            TransactionSynchronizationManager.bindResource(dataSource, holderToUse);  
        }  
    }  
    return con;  
}  

關(guān)于數(shù)據(jù)庫操作類RDBMS
從JdbcTemplate中,我們看到,他提供了許多簡單查詢和更新的功能。但是,如果需要更高層次的抽象,以及更面向?qū)ο蟮姆椒▉碓L問數(shù)據(jù)庫,Spring為我們提供了org.springframework.jdbc.object包,里面包含了SqlQuery、SqlMappingQuery、SqlUpdate和StoredProcedure等類,這些類都是Spring JDBC應(yīng)用程序可以使用的。但要注意,在使用這些類時需要為它們配置好JdbcTemplate作為其基本的操作實現(xiàn),因為在它們的功能實現(xiàn)中,對數(shù)據(jù)庫操作的那部分實現(xiàn)基本上還是依賴于JdbcTemplate來完成的。

比如,對MappingSqlQuery使用的過程,是非常簡潔的;在設(shè)計好數(shù)據(jù)的映射代碼之后,查詢得到的記錄已經(jīng)按照前面的設(shè)計轉(zhuǎn)換為對象List了,一條查詢記錄對應(yīng)于一個數(shù)據(jù)對象,可以把數(shù)據(jù)庫的數(shù)據(jù)記錄直接映射成Java對象在程序中使用,同時又可避免使用第三方ORM工具的配置,對于簡單的數(shù)據(jù)映射場合是非常方便的;在mapRow方法的實現(xiàn)中提供的數(shù)據(jù)轉(zhuǎn)換規(guī)則,和我們使用Hibernate時,Hibernate的hbm文件起到的作用是非常類似的。這個MappingSqlQuery需要的對設(shè)置進行compile,這些compile是這樣完成的,如以下代碼所示:

protected final void compileInternal() {  
    //這里是對參數(shù)的compile過程,所有的參數(shù)都在getDeclaredParameters里面,生成了一個PreparedStatementCreatorFactory  
    this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());  
    this.preparedStatementFactory.setResultSetType(getResultSetType());  
    this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());  
    this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());  
    if (getGeneratedKeysColumnNames() != null) {  
        this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());  
    }  
his.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());  
    onCompileInternal();  

在執(zhí)行查詢時,執(zhí)行的實際上是SqlQuery的executeByNamedParam方法,這個方法需要完成的工作包括配置SQL語句,配置數(shù)據(jù)記錄到數(shù)據(jù)對象的轉(zhuǎn)換的RowMapper,然后使用JdbcTemplate來完成數(shù)據(jù)的查詢,并啟動數(shù)據(jù)記錄到Java數(shù)據(jù)對象的轉(zhuǎn)換,如以下代碼所示:

public List<T> executeByNamedParam(Map<String, ?> paramMap, Map context) throws DataAccessException {  
    validateNamedParameters(paramMap);  
    //得到需要執(zhí)行的SQL語句  
    ParsedSql parsedSql = getParsedSql();  
    MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);  
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);  
    //配置好SQL語句需要的Parameters及rowMapper,這個rowMapper完成數(shù)據(jù)記錄到對象的轉(zhuǎn)換  
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());  
    RowMapper<T> rowMapper = newRowMapper(params, context);  
    //我們又看到了JdbcTemplate,這里使用JdbcTemplate來完成對數(shù)據(jù)庫的查詢操作,所以我們說JdbcTemplate是非常基本的操作類  
        return getJdbcTemplate().query(newPreparedStatementCreator(sqlToUse, params), rowMapper);  
}  

在Spring對JDBC的操作中,基本上是對JDBC/Hibernate基礎(chǔ)上API的封裝。這些封裝可以直接使用,也可以在IoC容器中配置好了再使用,當(dāng)結(jié)合IoC容器的基礎(chǔ)上進行使用的時候,可以看到許多和事務(wù)管理相關(guān)的處理部分,都是非常值得學(xué)習(xí)的,在那里,可以看到對數(shù)據(jù)源的管理 - Hibernate中session的管理,與線程的結(jié)合等等。

名稱欄目:深入解析Spring架構(gòu)與設(shè)計原理-數(shù)據(jù)庫的操作實現(xiàn)
標(biāo)題鏈接:http://bm7419.com/article4/igdhoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護、搜索引擎優(yōu)化軟件開發(fā)App開發(fā)、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司