欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

深入解析Spring架構(gòu)與設(shè)計(jì)原理-數(shù)據(jù)庫(kù)的操作實(shí)現(xiàn)-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司專(zhuān)注于武川企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站制作。武川網(wǎng)站建設(shè)公司,為武川等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站策劃,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)

//execute方法執(zhí)行的是輸入的sql語(yǔ)句
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語(yǔ)句的方法
public <T> T execute(StatementCallback<T> action) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");
    //這里取得數(shù)據(jù)庫(kù)的Connection,這個(gè)數(shù)據(jù)庫(kù)的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ù)庫(kù)異常,把數(shù)據(jù)庫(kù)Connection釋放,同時(shí)拋出一個(gè)經(jīng)過(guò)Spring轉(zhuǎn)換過(guò)的Spring數(shù)據(jù)庫(kù)異常
        //Spring做了一項(xiàng)有意義的工作,就是把這些數(shù)據(jù)庫(kù)異常統(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ù)庫(kù)connection
        DataSourceUtils.releaseConnection(con, getDataSource());
    }
}

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

//這是取得數(shù)據(jù)庫(kù)連接的調(diào)用,實(shí)現(xiàn)是通過(guò)調(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");
    //把對(duì)數(shù)據(jù)庫(kù)的Connection放到事務(wù)管理中進(jìn)行管理,這里使用TransactionSynchronizationManager中定義的ThreadLocal變量來(lái)和線程綁定數(shù)據(jù)庫(kù)連接
    //如果在TransactionSynchronizationManager中已經(jīng)有與當(dāng)前線程綁定數(shù)據(jù)庫(kù)連接,那就直接取出來(lái)使用
    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ù)庫(kù)Connection,在Bean配置文件中定義好的,
    // 同時(shí)最后把新打開(kāi)的數(shù)據(jù)庫(kù)Connection通過(guò)TransactionSynchronizationManager和當(dāng)前線程綁定起來(lái)。
    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ù)庫(kù)操作類(lèi)RDBMS
從JdbcTemplate中,我們看到,他提供了許多簡(jiǎn)單查詢和更新的功能。但是,如果需要更高層次的抽象,以及更面向?qū)ο蟮姆椒▉?lái)訪問(wèn)數(shù)據(jù)庫(kù),Spring為我們提供了org.springframework.jdbc.object包,里面包含了SqlQuery、SqlMappingQuery、SqlUpdate和StoredProcedure等類(lèi),這些類(lèi)都是Spring JDBC應(yīng)用程序可以使用的。但要注意,在使用這些類(lèi)時(shí)需要為它們配置好JdbcTemplate作為其基本的操作實(shí)現(xiàn),因?yàn)樵谒鼈兊墓δ軐?shí)現(xiàn)中,對(duì)數(shù)據(jù)庫(kù)操作的那部分實(shí)現(xiàn)基本上還是依賴于JdbcTemplate來(lái)完成的。

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

protected final void compileInternal() {
    //這里是對(duì)參數(shù)的compile過(guò)程,所有的參數(shù)都在getDeclaredParameters里面,生成了一個(gè)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í)行查詢時(shí),執(zhí)行的實(shí)際上是SqlQuery的executeByNamedParam方法,這個(gè)方法需要完成的工作包括配置SQL語(yǔ)句,配置數(shù)據(jù)記錄到數(shù)據(jù)對(duì)象的轉(zhuǎn)換的RowMapper,然后使用JdbcTemplate來(lái)完成數(shù)據(jù)的查詢,并啟動(dòng)數(shù)據(jù)記錄到Java數(shù)據(jù)對(duì)象的轉(zhuǎn)換,如以下代碼所示:

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

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

更多內(nèi)容請(qǐng)關(guān)注微信公眾號(hào):IT哈哈(it_haha)

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章題目:深入解析Spring架構(gòu)與設(shè)計(jì)原理-數(shù)據(jù)庫(kù)的操作實(shí)現(xiàn)-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article2/dhicic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)外貿(mào)網(wǎng)站建設(shè)網(wǎng)站改版做網(wǎng)站自適應(yīng)網(wǎng)站企業(yè)網(wǎng)站制作

廣告

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

成都做網(wǎng)站