<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

       :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

    五、Spring 持久層

    對于不同的數據庫技術,某些步驟是固定的,只有少部分不同。Spring運用Template Method模式,將固定的流程編寫與Temp類(JdbcTemplateHibernateTemplate)之中,對不同的一些細節步驟,依托特定DAO支持對象來處理。

    SpringDAO框架并不拋出與數據庫技術相關的異常,Spring所有異常都是DataAccessException的子類,一個與數據庫技術無關的通用異常類,該類繼承至RuntimeException

    對于Jdbc存取,SpringSQLException等轉化為自己的DAO異常對象。

    DataSource注入

    對于不同的數據庫鏈接來源需求,Spring提供了javax.sql.DataSource注入,更換數據來源只需在Bean定義文件中修改配置,不需修改程序代碼。
    例如可以在Bean文件中編寫如下:

     1<!-- 帶連接池的DataSource -->
     2<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     3    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
     4    <property name="url" value="jdbc:mysql://localhost:3306/test" />
     5    <property name="username" value="root" />
     6    <property name="password" value="root" />
     7</bean>
     8    
     9<bean id="peopleDao" class="SpringPersistence.PeopleDaoMySQLImpl">
    10    <property name="dataSource" ref="dataSource" />
    11</bean>

    要建立JdbcTemplate的實例,

     1public class PeopleDaoMySQLImpl implements PeopleDao {
     2    
     3    private DataSource dataSource;
     4    
     5    public void setDataSource(DataSource dataSource){
     6        this.dataSource = dataSource;
     7    }

     8
     9    public void save(People people) {
    10        Connection conn = null;
    11        Statement stmt = null;
    12        try {
    13            conn = dataSource.getConnection();
    14            stmt = conn.createStatement();
    15            stmt.execute("INSERT INTO people VALUES (" + people.getId()
    16                    + ",'" + people.getName() + "'," + people.getAge() + ")");
    17            stmt.execute("");
    18        }
     catch (SQLException e) {
    19            e.printStackTrace();
    20        }
     finally {
    21            if(stmt != null){
    22                try {
    23                    stmt.close();
    24                }
     catch (SQLException e) {
    25                    e.printStackTrace();
    26                }

    27            }
        
    28            if(conn != null){
    29                try {
    30                    conn.close();
    31                }
     catch (SQLException e) {
    32                    e.printStackTrace();
    33                }

    34            }

    35        }

    36    }

    37}


        使用JdbcTemplate

    JdbcTemplate封裝了Connection的取得,Statement的建立,異常的處理,Statement的關閉,Connection的關閉等。它被設計為線程安全的。

    要建立JdbcTemplate的實例,必須要有一個DataSource對象作為構造對象。

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    Spring事務管理

    Spring提供編程式事務管理與聲明式事務管理。

    編程式事務管理

    Spring提供了兩種方式實現編程式事務管理:使用PlatformTransactionManager實現;使用org.springframework.transaction.support.TransactionTemplate

        PlatformTransactionManager接口定義如下:

    1public class interface PlatformTransactionManager {
    2    public void commit(TransactionStatus status) throws TransactionException;
    3    public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
    4    public void rollback(TransactionStatus arg0) throws TransactionException;
    5}

    DataSourceTransactionManager是其一個實現類。

    修改添加新用戶的代碼段作為示例:

     1public class PeopleDaoMySQLImpl2 implements PeopleDao {
     2
     3    private DataSource dataSource;
     4    private DataSourceTransactionManager transactionManager;
     5    private DefaultTransactionDefinition def;
     6    private JdbcTemplate jdbcTemplate;
     7
     8    public void setDataSource(DataSource dataSource) {
     9        this.dataSource = dataSource;
    10        jdbcTemplate = new JdbcTemplate(dataSource);
    11        transactionManager = new DataSourceTransactionManager(dataSource);
    12        def = new DefaultTransactionDefinition();
    13        def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
    14    }

    15
    16    public void save(People people) {
    17        TransactionStatus status = transactionManager.getTransaction(def);
    18        try {
    19            jdbcTemplate.execute("INSERT INTO people VALUES (" + people.getId()
    20                        + ",'" + people.getName() + "'," + people.getAge() + ")");
    21            //jdbcTemplate.execute("");
    22        }
     catch (DataAccessException e) {
    23            transactionManager.rollback(status);
    24            throw e;
    25        }

    26        transactionManager.commit(status);
    27    }

    28}

    另一個編程式事務管理方法是使用TransactionTemplate,它需要一個TransactionManager實例,如下:

     1public class PeopleDaoMySQLImpl3 implements PeopleDao {
     2
     3    private DataSource dataSource;
     4    private DataSourceTransactionManager transactionManager;
     5    private JdbcTemplate jdbcTemplate;
     6
     7    public void setDataSource(DataSource dataSource) {
     8        this.dataSource = dataSource;
     9        jdbcTemplate = new JdbcTemplate(dataSource);
    10        transactionManager = new DataSourceTransactionManager(dataSource);
    11    }

    12
    13    public void save(People people) {
    14        final int id = people.getId();
    15        final String name = people.getName();
    16        final int age = people.getAge();
    17        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    18        transactionTemplate.execute(new TransactionCallbackWithoutResult(){
    19            public void doInTransactionWithoutResult(TransactionStatus status){
    20                try {
    21                    jdbcTemplate.execute("INSERT INTO people VALUES (" + id
    22                            + ",'" + name + "'," + age + ")");
    23                    //jdbcTemplate.execute("");
    24                }
     catch (DataAccessException e) {
    25                    status.setRollbackOnly();
    26                }

    27            }

    28        }
    );
    29    }

    30}

    聲明式事務管理

    Spring的聲明式事務管理依賴于它的AOP框架來完成,使用聲明式的事務管理的好處是,事務管理不侵入開發的組件。

    定義文件如下:

     1<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     2    <property name="dataSource" ref="dataSource" />
     3</bean>
     4
     5<bean id="peopleDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
     6    <property name="proxyInterfaces">
     7        <list>
     8            <value>SpringPersistence.PeopleDao</value>
     9        </list>
    10    </property>
    11    <property name="target" ref="peopleDao"/>
    12    <property name="transactionManager" ref="transactionManager" />
    13    <property name="transactionAttributes">
    14        <props>
    15            <prop key="sa*">PROPAGATION_REQUIRED</prop>
    16        </props>
    17    </property>
    18</bean>



    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久久久亚洲精品影视| 国产成人高清精品免费观看| 高清一区二区三区免费视频| 国产AV无码专区亚洲AWWW| 一级做受视频免费是看美女 | 久九九精品免费视频| 中文字幕亚洲第一在线| 最近免费2019中文字幕大全| 亚洲美女视频网址| 1024免费福利永久观看网站| 国产精品亚洲午夜一区二区三区| 色婷婷7777免费视频在线观看| 亚洲一区动漫卡通在线播放| 成人性生交视频免费观看| 九九精品国产亚洲AV日韩| 免费一看一级毛片| 国产线视频精品免费观看视频| 久久精品亚洲综合专区| 免费成人福利视频| 国产精品国产亚洲区艳妇糸列短篇| 日韩免费视频观看| 在线视频网址免费播放| 久久综合亚洲色一区二区三区| 免费做爰猛烈吃奶摸视频在线观看| 国产成人精品日本亚洲语音 | 久久综合久久综合亚洲| 国产乱子伦精品免费无码专区| 国产日韩AV免费无码一区二区三区| 久久亚洲国产精品一区二区| 桃子视频在线观看高清免费完整| 韩国亚洲伊人久久综合影院| 亚洲精品~无码抽插| 国产又黄又爽又猛免费app| 羞羞漫画小舞被黄漫免费| 亚洲国产成人一区二区精品区| 成全视频免费高清 | 无码日韩精品一区二区三区免费| 亚洲第一男人天堂| 亚洲精品中文字幕无码蜜桃| 成年女性特黄午夜视频免费看 | 亚洲日本中文字幕天堂网|