如何详细配置SSM框架中Spring整合MyBatis的事务管理?

2026-05-16 07:242阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计691个文字,预计阅读时间需要3分钟。

如何详细配置SSM框架中Spring整合MyBatis的事务管理?

SSM整合Spring框架配置事务

1. 在applicationContext.xml中修改代码如下:

xml

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

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

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:aop="www.springframework.org/schema/aop" xmlns:tx="www.springframework.org/schema/tx" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop.xsd www.springframework.org/schema/tx 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.localhost:8080/如图所示:

6.填写姓名和金额如图所示:

7.点击保存会跳转到如图所示的界面:

8.控制台打印结果如图所示:

到此这篇关于ssm整合之Spring整合MyBatis框架配置事务的文章就介绍到这了,更多相关Spring整合MyBatis配置事务内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

如何详细配置SSM框架中Spring整合MyBatis的事务管理?

本文共计691个文字,预计阅读时间需要3分钟。

如何详细配置SSM框架中Spring整合MyBatis的事务管理?

SSM整合Spring框架配置事务

1. 在applicationContext.xml中修改代码如下:

xml

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

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

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:aop="www.springframework.org/schema/aop" xmlns:tx="www.springframework.org/schema/tx" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop.xsd www.springframework.org/schema/tx 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.localhost:8080/如图所示:

6.填写姓名和金额如图所示:

7.点击保存会跳转到如图所示的界面:

8.控制台打印结果如图所示:

到此这篇关于ssm整合之Spring整合MyBatis框架配置事务的文章就介绍到这了,更多相关Spring整合MyBatis配置事务内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

如何详细配置SSM框架中Spring整合MyBatis的事务管理?