<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之旅 廣告
                **1. 在Spring配置文件`ApplicationContext.xml`中配置事務管理器** ```xml <!-- 1. 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 2. 事務增強 --> <tx:advice id="txAdvice"> <!-- 3. 定義事務規則 --> <tx:attributes> <!-- 對所有以insert、batchInsert、del、update開頭的方法添加事務 --> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="batchInsert*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 4. 定義切面 --> <aop:config> <!-- 5. 定義切入點 --> <aop:pointcut id="serviceMethod" expression="execution(* com.learn.spring07.service..*.*(..))" /> <!-- 6. 應用事務增強 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config> ``` **2. 上面配置好后,在業務層中對應的方法就會有事務了** ```java @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override public int batchInsert(List<Student> studentList) { for (int i = 0; i < studentList.size(); i++) { Student student = studentList.get(i); if (i == 4) { int d = 10 / 0; //除0異常 } studentDao.insert(student); } return studentList.size(); } } ``` **3. 測試事務效果** ```java @Test public void batchInsert() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); StudentService studentService = context.getBean(StudentService.class); List<Student> studentList = new ArrayList<>(); studentList.add(new Student("李四", 20, "男")); studentList.add(new Student("王五", 21, "男")); studentList.add(new Student("趙六", 22, "男")); studentList.add(new Student("田七", 23, "男")); studentList.add(new Student("周八", 24, "男")); int result = studentService.batchInsert(studentList); System.out.println(result); } ``` 當插入第4條數據時發生除0異常,查看數據庫沒有被任何修改,事務配置成功。
                  <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>

                              哎呀哎呀视频在线观看