ssm整合后配置的事务不起作用的原因和解决方法

在完成ssm整合后,配置了事务,但是发现事务没有起作用

spring的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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 开启注解扫描 -->
	<context:component-scan
		base-package="com.bailiban" />
	<!-- 加载配置文件 -->
	<context:property-placeholder
		location="classpath:db.properties" />
	<!--注入数据源spring整合mybatis -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- 配置SqlSessionFactory工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<!-- 取别名 -->
	<property name="typeAliasesPackage" value="com.bailiban.pojo"></property>
	<!-- 加载mybatis配置文件 -->
	<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	</bean>
	<!-- 配置扫描包 -->
	<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.bailiban.mapper"/>
	</bean>
	<!-- 注入事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启事务 -->
	<tx:annotation-driven />
</beans>

springmvc的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="com.bailiban"/>
        <!-- 配置视图解析器 -->
        <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
          <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 静态资源不拦截 -->
        <mvc:resources location="/css/" mapping="/css/**"/>
         <mvc:resources location="/js/" mapping="/js/**"/>
          <mvc:resources location="/images/" mapping="/images/**"/>
          <!--  加载驱动-->
          <mvc:annotation-driven/>
</beans>

代码结构如下图:

测试事务代码,在Service业务层加上如下异常

 

最终测试,发现抛出异常,但是事务并没有回滚,成功保存!

原因 :springmvc和spring的配置扫描包,均是扫描所有com.bailiban 下面的包,而springmvc只需要扫描Controller层,即com.bailiban.controller即可。

具体解释如下:(摘自:引用

1、Spring与SpringMVC属于父子容器关系。框架启动时先启动Spring容器,而后启动SpringMVC容器。子容器可以访问父容器中的Bean,而父容器不能访问子容器中的Bean。

2、由于SpringMVC在扫描时扩大了扫描范围,装载了@Service标识的类的实例,从而导致Controller层在注入Service时,实际注入的时子容器中的Service实例。

3、事务被配置在父容器中,Spring父容器在装载Service时会同时应用事务配置,而SpringMVC只是单纯加载Service的实例。

解决方法: 将springmvc中的扫描范围缩小到controller层即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="com.bailiban.controller"/>
        <!-- 配置视图解析器 -->
        <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
          <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 静态资源不拦截 -->
        <mvc:resources location="/css/" mapping="/css/**"/>
         <mvc:resources location="/js/" mapping="/js/**"/>
          <mvc:resources location="/images/" mapping="/images/**"/>
          <!--  加载驱动-->
          <mvc:annotation-driven/>
</beans>

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SSM(Spring+SpringMVC+MyBatis)是目前Java Web开发常用的一种技术框架,整合起来可以提高开发效率和代码可维护性。下面是SSM整合的注意事项和步骤: 注意事项: 1. 版本匹配:Spring、SpringMVC、MyBatis的版本需要匹配,否则会出现兼容性问题。 2. 配置文件路径:Spring、SpringMVC、MyBatis的配置文件需要放在不同的目录下,否则会出现路径错误。 3. 包扫描路径:Spring、SpringMVC的包扫描路径需要配置正确,否则会出现无法注入依赖的错误。 4. 数据库连接信息:MyBatis需要正确配置数据库连接信息,否则会出现数据库访问错误。 步骤: 1. 创建Maven项目,并添加相关依赖。 2. 配置web.xml文件,配置DispatcherServlet和ContextLoaderListener。 3. 配置Spring配置文件,包括数据源、事务管理器、MapperScannerConfigurer等。 4. 配置SpringMVC配置文件,包括视图解析器、静态资源处理器、RequestMappingHandlerMapping等。 5. 配置MyBatis配置文件,包括数据源、Mapper映射文件等。 6. 在Mapper映射文件编写SQL语句,并在DAO接口定义对应的方法。 7. 在Service层调用DAO层方法,实现业务逻辑。 8. 编写Controller层方法,处理前端请求,调用Service层方法,并返回视图。 9. 编写前端页面,实现用户交互和展示数据。 以上是SSM整合的基本步骤和注意事项,具体实现过程可以参考相关教程和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值