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("表现层:查询所有账户...");
    // 调用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配置事务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java使用Sftp和Ftp实现对文件的上传和下载

    Java使用Sftp和Ftp实现对文件的上传和下载

    这篇文章主要介绍了Java使用Sftp和Ftp实现对文件的上传和下载,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • SpringBoot配置多数据源的四种方式分享

    SpringBoot配置多数据源的四种方式分享

    在日常开发中我们都是以单个数据库进行开发,在小型项目中是完全能够满足需求的,但是,当我们牵扯到大型项目的时候,单个数据库就难以承受用户的CRUD操作,那么此时,我们就需要使用多个数据源进行读写分离的操作,本文就给大家介绍SpringBoot配置多数据源的方式
    2023-07-07
  • java并发高的情况下用ThreadLocalRandom来生成随机数

    java并发高的情况下用ThreadLocalRandom来生成随机数

    如果我们想要生成一个随机数,通常会使用Random类。但是在并发情况下Random生成随机数的性能并不是很理想,本文主要介绍了java并发高的情况下用ThreadLocalRandom来生成随机数,感兴趣的可以了解一下
    2022-05-05
  • java与微信小程序实现websocket长连接

    java与微信小程序实现websocket长连接

    这篇文章主要为大家详细介绍了java与微信小程序实现websocket长连接,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 实例讲解JAVA 适配器模式

    实例讲解JAVA 适配器模式

    这篇文章主要介绍了JAVA 适配器模式的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2020-06-06
  • java组件commons-fileupload实现文件上传、下载、在线打开

    java组件commons-fileupload实现文件上传、下载、在线打开

    这篇文章主要介绍了java组件commons-fileupload实现文件上传、下载、在线打开,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • 详细介绍Java函数式接口

    详细介绍Java函数式接口

    函数式接口在Java中是指有且仅有一个抽象方法的接口。当然接口中可以包含其他的方法默认、静态、私有,具体内容请参考下面文章内容
    2021-09-09
  • springboot指定profiles启动失败问题及解决

    springboot指定profiles启动失败问题及解决

    这篇文章主要介绍了springboot指定profiles启动失败问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • SpringBoot中WEB的启动流程分析

    SpringBoot中WEB的启动流程分析

    今天我们就来分析下springboot启动web项目整个流程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2022-03-03
  • java数字和中文算数验证码的实现

    java数字和中文算数验证码的实现

    这篇文章主要介绍了java数字和中文算数验证码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论