欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年10月23日 09:18:52   作者:學(xué)無止路  
這篇文章主要介紹了ssm整合之Spring整合MyBatis框架配置事務(wù),本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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增強-->
  <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>首頁</title>
</head>
<body>
  <a href="account/findAll" rel="external nofollow" >測試查詢</a>
  <h3>測試保存</h3>
  <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 // 自動類型注入
  private AccountService accountService;
  @RequestMapping(value = "/findAll")
  public String findAll(Model model){
    System.out.println("表現(xiàn)層:查詢所有賬戶...");
    // 調(diào)用findAll()方法
    List<Account> list = accountService.findAll();
    // 進行存儲
    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.重新部署項目,運行如圖所示:

在這里插入圖片描述

5.通過瀏覽器訪問http://localhost:8080/如圖所示:

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

相關(guān)文章

  • @CacheEvict 清除多個key的實現(xiàn)方式

    @CacheEvict 清除多個key的實現(xiàn)方式

    這篇文章主要介紹了@CacheEvict 清除多個key的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java ByteBuffer網(wǎng)絡(luò)編程用法實例解析

    Java ByteBuffer網(wǎng)絡(luò)編程用法實例解析

    這篇文章主要介紹了Java ByteBuffer網(wǎng)絡(luò)編程用法實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Java中的Apache?Commons?Math使用詳解

    Java中的Apache?Commons?Math使用詳解

    Java中的Apache?Commons?Math是一個開源的數(shù)學(xué)庫,它提供了許多常用的數(shù)學(xué)函數(shù)和算法,這個庫對于需要處理大量數(shù)據(jù)的開發(fā)者來說非常有用,因為它可以大大簡化代碼并提高效率,本文給大家詳解講解Java中的Apache?Commons?Math知識,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • Java顯式鎖詳情

    Java顯式鎖詳情

    這篇文章主要詳細(xì)的介紹了shenJava顯式鎖常用的api及標(biāo)準(zhǔn)用法,感興趣的朋友,需要的朋友可以參考下面文章里的內(nèi)容
    2021-09-09
  • 詳解springcloud之服務(wù)注冊與發(fā)現(xiàn)

    詳解springcloud之服務(wù)注冊與發(fā)現(xiàn)

    本次分享的是關(guān)于springcloud服務(wù)注冊與發(fā)現(xiàn)的內(nèi)容,將通過分別搭建服務(wù)中心,服務(wù)注冊,服務(wù)發(fā)現(xiàn)來說明,非常具有實用價值,需要的朋友可以參考下
    2018-06-06
  • 新手入門學(xué)習(xí)Spring Freemarker教程解析

    新手入門學(xué)習(xí)Spring Freemarker教程解析

    這篇文章主要介紹了新手入門學(xué)習(xí)Freemarker教程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Java數(shù)組使用binarySearch()方法查找指定元素的實現(xiàn)

    Java數(shù)組使用binarySearch()方法查找指定元素的實現(xiàn)

    這篇文章主要介紹了Java數(shù)組使用binarySearch()方法查找指定元素的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Spring?Data?JPA框架的Repository自定義實現(xiàn)詳解

    Spring?Data?JPA框架的Repository自定義實現(xiàn)詳解

    Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的Repository自定義實現(xiàn)
    2022-04-04
  • Java-lambda表達(dá)式入門看這一篇就夠了

    Java-lambda表達(dá)式入門看這一篇就夠了

    lambda表達(dá)式最簡單的作用就是用于簡化創(chuàng)建匿名內(nèi)部類對象,Lambda表達(dá)式是一個可傳遞的代碼塊,可以在以后執(zhí)行一次或多次,下面通過本文給大家介紹Java-lambda表達(dá)式入門教程,感興趣的朋友一起看看吧
    2021-05-05
  • SpringBoot配置綁定方法詳解

    SpringBoot配置綁定方法詳解

    配置綁定是SpringBoot其中一個底層功能,SpringBoot把配置綁定的過程變得更加簡單,傳統(tǒng)java將常用的配置放到配置文件properties中,之后將這些配置綁定到j(luò)avabean中
    2022-10-10

最新評論