最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ssm整合之Spring整合MyBatis框架配置事務的詳細教程

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

ssm整合之Spring整合MyBatis框架配置事務

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框架聲明式事務管理-->
  <!--配置事務管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!--配置事務通知-->
  <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.點擊保存會跳轉到如圖所示的界面:

在這里插入圖片描述

8.控制臺打印結果如圖所示:

在這里插入圖片描述

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

相關文章

  • Java使用Sftp和Ftp實現(xiàn)對文件的上傳和下載

    Java使用Sftp和Ftp實現(xiàn)對文件的上傳和下載

    這篇文章主要介紹了Java使用Sftp和Ftp實現(xiàn)對文件的上傳和下載,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • SpringBoot配置多數(shù)據(jù)源的四種方式分享

    SpringBoot配置多數(shù)據(jù)源的四種方式分享

    在日常開發(fā)中我們都是以單個數(shù)據(jù)庫進行開發(fā),在小型項目中是完全能夠滿足需求的,但是,當我們牽扯到大型項目的時候,單個數(shù)據(jù)庫就難以承受用戶的CRUD操作,那么此時,我們就需要使用多個數(shù)據(jù)源進行讀寫分離的操作,本文就給大家介紹SpringBoot配置多數(shù)據(jù)源的方式
    2023-07-07
  • java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)

    java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)

    如果我們想要生成一個隨機數(shù),通常會使用Random類。但是在并發(fā)情況下Random生成隨機數(shù)的性能并不是很理想,本文主要介紹了java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù),感興趣的可以了解一下
    2022-05-05
  • java與微信小程序?qū)崿F(xiàn)websocket長連接

    java與微信小程序?qū)崿F(xiàn)websocket長連接

    這篇文章主要為大家詳細介紹了java與微信小程序?qū)崿F(xiàn)websocket長連接,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 實例講解JAVA 適配器模式

    實例講解JAVA 適配器模式

    這篇文章主要介紹了JAVA 適配器模式的的相關資料,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下
    2020-06-06
  • java組件commons-fileupload實現(xiàn)文件上傳、下載、在線打開

    java組件commons-fileupload實現(xiàn)文件上傳、下載、在線打開

    這篇文章主要介紹了java組件commons-fileupload實現(xiàn)文件上傳、下載、在線打開,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 詳細介紹Java函數(shù)式接口

    詳細介紹Java函數(shù)式接口

    函數(shù)式接口在Java中是指有且僅有一個抽象方法的接口。當然接口中可以包含其他的方法默認、靜態(tài)、私有,具體內(nèi)容請參考下面文章內(nèi)容
    2021-09-09
  • springboot指定profiles啟動失敗問題及解決

    springboot指定profiles啟動失敗問題及解決

    這篇文章主要介紹了springboot指定profiles啟動失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringBoot中WEB的啟動流程分析

    SpringBoot中WEB的啟動流程分析

    今天我們就來分析下springboot啟動web項目整個流程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-03-03
  • java數(shù)字和中文算數(shù)驗證碼的實現(xiàn)

    java數(shù)字和中文算數(shù)驗證碼的實現(xiàn)

    這篇文章主要介紹了java數(shù)字和中文算數(shù)驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07

最新評論

樟树市| 东台市| 云阳县| 花垣县| 兰考县| 红安县| 建平县| 白朗县| 蕉岭县| 泾川县| 舞钢市| 崇阳县| 武平县| 宁远县| 嘉禾县| 衡山县| 开平市| 泗阳县| 青浦区| 武夷山市| 和平县| 定日县| 墨玉县| 嘉黎县| 连山| 井研县| 蓬溪县| 沂水县| 长葛市| 壶关县| 临夏市| 塔城市| 阿勒泰市| 鱼台县| 吕梁市| 定边县| 新密市| 浙江省| 达孜县| 鹤峰县| 潼南县|