<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                Spring的聲明式事務Annotation的實現------AOP應用 首先先討論一個問題: **事務放在service層還是放在dao層呢?** Dao層只是針對于某一個實體進行CRUD的操作。如果在增加刪除一個表的時候。比如USER我們要在LOG表中記錄相應的日志。那么在DAO層如何完成。所以說Service層可能操作多個實體類。所以事務加載service層比較合適。因為不論我們保存哪一個實例出現了錯誤我們都可以回滾。而不是Log插入了,User沒插入。反之亦然,這都是我們不愿意看到的。 下面舉例Spring中事務通過Annotation方式是如何實現的: **1、??加入tx開頭的命名空間并且配置事務manager和driven。代碼如下:** ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <!-- 配置容器資源掃描的包 --> <context:component-scan base-package="com.spring" /> <!-- 將前面類寫入容器 --> <bean id="logInterceptor" class="com.spring.aop.LogInterceptor" /> <!-- 配置數據源 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/sms"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> --> <!-- placeholder 占位符 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- 配置dataSource --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 將配置好的dataSource注入到SessionFactory中--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/spring/model/user.hbm.xml</value> <value>com/spring/model/userlog.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.hbm2ddl.auto=create </value> </property> </bean> <!-- 聲明式事務管理,事務需要數據源,從sessionFactory中拿到 這是一個AOP的應用 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 聲明式事務注解的配置 --> <tx:annotation-driven /> </beans> ~~~ **2、在Java文件上使用注解標簽:** ~~~ //在Service層加入事務的控制。具體邏輯不在給予。 //在下面方法前面加邏輯 /*事務的傳播性。如果當前執行環境中有事務,那么則會一直在 * 傳播環境中傳播下去。如果沒有事務那么則會創建一個事務 */ @Transactional(propagation=Propagation.REQUIRED) public void save(User u){ u.setName("zhanglong"); UserLog log = new UserLog(); log.setMsg("user added"); userDaoImpl.save(u); userLogDaoImpl.save(log); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看