欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、中衛(wèi)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為中衛(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,這個(gè)數(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釋放,同時(shí)拋出一個(gè)經(jīng)過Spring轉(zhuǎn)換過的Spring數(shù)據(jù)庫異常  
        //Spring做了一項(xiàng)有意義的工作,就是把這些數(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í)候,有一個(gè)很重要的地方就是對(duì)數(shù)據(jù)庫連接的管理,在這里,是由DataSourceUtils來完成的。Spring通過這個(gè)輔助類來對(duì)數(shù)據(jù)的Connection進(jìn)行管理。比如通過它來完成打開和關(guān)閉Connection等操作。DataSourceUtils對(duì)這些數(shù)據(jù)庫Connection管理的實(shí)現(xiàn), 如以下代碼所示。

//這是取得數(shù)據(jù)庫連接的調(diào)用,實(shí)現(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");  
    //把對(duì)數(shù)據(jù)庫的Connection放到事務(wù)管理中進(jìn)行管理,這里使用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í)最后把新打開的數(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)用程序可以使用的。但要注意,在使用這些類時(shí)需要為它們配置好JdbcTemplate作為其基本的操作實(shí)現(xiàn),因?yàn)樵谒鼈兊墓δ軐?shí)現(xiàn)中,對(duì)數(shù)據(jù)庫操作的那部分實(shí)現(xiàn)基本上還是依賴于JdbcTemplate來完成的。

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

protected final void compileInternal() {  
    //這里是對(duì)參數(shù)的compile過程,所有的參數(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語句,配置數(shù)據(jù)記錄到數(shù)據(jù)對(duì)象的轉(zhuǎn)換的RowMapper,然后使用JdbcTemplate來完成數(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語句  
    ParsedSql parsedSql = getParsedSql();  
    MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);  
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);  
    //配置好SQL語句需要的Parameters及rowMapper,這個(gè)rowMapper完成數(shù)據(jù)記錄到對(duì)象的轉(zhuǎn)換  
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());  
    RowMapper<T> rowMapper = newRowMapper(params, context);  
    //我們又看到了JdbcTemplate,這里使用JdbcTemplate來完成對(duì)數(shù)據(jù)庫的查詢操作,所以我們說JdbcTemplate是非常基本的操作類  
        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é)合等等。

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

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

廣告

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

成都網(wǎng)頁設(shè)計(jì)公司
午夜神马福利激情视频| 激情一区二区三区视频| 国产三级三级三级三级三级| 久久综合色爱综合欧美| 日韩50岁老女人骚色| av一区二区三区网站| 91九色精品视频在线观看| 国产成人在线观看av| 国内精品自产拍久久久久久久久91| 国产老熟女高潮一区二区| 亚洲精品一区二区av| 偷拍一区二区三区免费| 日韩视频 一区 二区| 四虎最新地址在线观看| 在线观看视频网站一卡二卡 | 国产高潮呻吟久久av| 丝袜美腿一区二区三区动态图| 天天操天天射夜夜撸| 久久精品国产亚洲成人av| 在线免费观看国产不卡| 日本中文字幕有码专区| 欧美亚洲国产精品综合在线| 高质量的性生活在线观看| 青青草原免费在线观看| 国产精品三级玖玖玖电影| 国产伦奸在线播放免费| 国产亚洲精品麻豆一区二区| 自拍偷拍一区蜜桃视频| 国产中文精品字幕a区| 亚洲综合实力最强的国家| 亚洲香蕉av在线一区二区三区| 国产精品人妻在线av| 亚洲国产视频不卡一区| 国产av不卡二区三区| 激情影院在线观看福利| 人妻一区二区三区中文字幕| 中文字幕日韩有码在线| 免费欧美大片在线观看高清| 亚洲另类综合日韩一区| 97在线公开免费视频| 91成人伦理在线观看|