ssm整合过程问题(02,2019.7.26)

本文详细介绍了在SSM框架整合过程中遇到的事务管理问题,包括如何在Controller中使用Spring的事务管理,解决数据插入时的空白问题,以及如何通过在springMVC.xml配置文件中添加事务管理来实现数据的正确回滚。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整合ssm的问题(2)

(2)测试spring-springmvc整合时spring的事务管理无法在Controller中起作用的问题

在这里插入图片描述
插入数据的时候全是空白:
AccountController类:


@Transactional
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    /**
     * 查询所有数据
     */
    @RequestMapping("/test01")
    public ModelAndView test01() {
        ModelAndView mv = new ModelAndView();
        List<Account> allAccount = accountService.findAllAccount();
        mv.addObject("accounts", allAccount);
        mv.setViewName("success");
        return mv;
    }

    /**
     *  插入数据 
     */
    @RequestMapping("/test04")
    public void test04(HttpServletRequest request, HttpServletResponse response, Account account) throws IOException {
        accountService.insertAccount(account);
        response.sendRedirect(request.getContextPath() + "/account/test01");
    }
}

在这里插入图片描述
一开始没有添加< input >标签的name属性,导致注入为空
在这里插入图片描述
自己找遍了各种配置,没注意这个,找了一天的错误。。。。。

修改之后又发现,执行保存的时候可以保存数据了,但是发生异常数据不能回滚,添加了@Transaction注解,还是不行,最后在springMVC.xml的配置文件中需要加上事务管理

    <tx:annotation-driven transaction-manager="transactionManager"/>

即springMVC.xml(上面的约束需要修改,才能支持配置)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.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">

    <!-- 开启注解扫描-->
    <context:component-scan base-package="com.ccl.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 静态页面可以访问-->
    <mvc:default-servlet-handler/>
    <!-- 视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 开启SpringMVC框架注解的支持-->
    <mvc:annotation-driven/>
    
    <!-- 添加对Controller中事务管理-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

在这里插入图片描述
最后测试成功事务能够回滚
(spring中的事务配置必须正确!!!!!!!!!才能用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值