<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                **1. 在Spring配置文件`ApplicationContext.xml`中配置事務管理器** ```xml <!-- 1. 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 2. 開啟事務注解--> <tx:annotation-driven transaction-manager="transactionManager" /> ``` **2. 在service層的方法上添加注解`@Transactional`開啟事務** 你可以在接口方法上添加或者在接口實現類的方法上添加都可以。 ```java public interface StudentService { //在接口的方法上添加 @Transactional int batchInserts(List<Student> studentList); } ``` ```java @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override @Transactional //或者在接口實現類的方法上添加 public int batchInserts(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 batchInserts() { 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>

                              哎呀哎呀视频在线观看