溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程

發(fā)布時(shí)間:2020-10-25 07:37:37 來(lái)源:腳本之家 閱讀:160 作者:學(xué)無(wú)止路 欄目:開發(fā)技術(shù)

ssm整合之Spring整合MyBatis框架配置事務(wù)

1.在applicationContext.xml修改代碼如下:

<?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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--開啟注解的掃描,希望處理service和dao,controller不需要Spring框架去處理-->
  <context:component-scan base-package="com.txw">
    <!--配置哪些注解不掃描-->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  </context:component-scan>
  <!--Spring整合MyBatis框架-->
  <!--配置連接池-->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql:///ssm"/>
    <property name="user" value="root"/>
    <property name="password" value="123456"/>
  </bean>
  <!--配置SqlSessionFactory工廠-->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!--配置AccountDao接口所在包-->
  <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.txw.dao"/>
  </bean>
  <!--配置Spring框架聲明式事務(wù)管理-->
  <!--配置事務(wù)管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!--配置事務(wù)通知-->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="find*" read-only="true"/>
      <tx:method name="*" isolation="DEFAULT"/>
    </tx:attributes>
  </tx:advice>
  <!--配置AOP增強(qiáng)-->
  <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.txw.service.impl.*ServiceImpl.*(..))"/>
  </aop:config>
</beans>

2.修改index.jsp的代碼如下:

<%--
 Created by IntelliJ IDEA.
 User: Adair
 Date: 2020/7/8 0008
 Time: 14:26
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>首頁(yè)</title>
</head>
<body>
  <a href="account/findAll" rel="external nofollow" >測(cè)試查詢</a>
  <h4>測(cè)試保存</h4>
  <form action="account/save" method="post">
     姓名:<input type="text" name="name" /><br/>
     金額:<input type="text" name="money" /><br/>
     <input type="submit" value="保存"/><br/>
  </form>
</body>
</html>

3.修改帳戶的控制類的代碼如下:

package com.txw.controller;

import com.txw.domain.Account;
import com.txw.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 *帳戶的控制類
 * @author Adair
 */
@Controller
@RequestMapping(path = "/account")
@SuppressWarnings("all")   // 注解警告信息
public class AccountController {
  @Autowired // 自動(dòng)類型注入
  private AccountService accountService;
  @RequestMapping(value = "/findAll")
  public String findAll(Model model){
    System.out.println("表現(xiàn)層:查詢所有賬戶...");
    // 調(diào)用findAll()方法
    List<Account> list = accountService.findAll();
    // 進(jìn)行存儲(chǔ)
    model.addAttribute("list",list);
    return "list";
  }
  /**
   * 保存
   * @return
   */
  @RequestMapping("/save")
  public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws Exception {
    accountService.saveAccount(account);
    response.sendRedirect(request.getContextPath()+"/account/findAll");
    return;
  }
}

4.重新部署項(xiàng)目,運(yùn)行如圖所示:

ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程

5.通過(guò)瀏覽器訪問(wèn)http://localhost:8080/如圖所示:

ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程

6.填寫姓名和金額如圖所示:

ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程

7.點(diǎn)擊保存會(huì)跳轉(zhuǎn)到如圖所示的界面:

ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程

8.控制臺(tái)打印結(jié)果如圖所示:

ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程

到此這篇關(guān)于ssm整合之Spring整合MyBatis框架配置事務(wù)的文章就介紹到這了,更多相關(guān)Spring整合MyBatis配置事務(wù)內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI