急急急!!!springMVC 注解事务失效
  • spring-hibernate.xml
    *<?xml version="1.0" encoding="UTF-8"?>
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.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-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    ">
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    com.mysql.jdbc.Driver


    jdbc:mysql://localhost:3306/shuxiang?useUnicode=true&characterEncoding=UTF8



    root


    sxmd

    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">



    org.hibernate.dialect.MySQLDialect
    update
    false




    com.demo.model


    class="org.springframework.orm.hibernate4.HibernateTransactionManager">


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


  • spring-servlet.xml
    <?xml version="1.0" encoding="UTF-8"?>
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.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-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.2.xsd
    ">
    <!-- 自动扫描的包名 -->





    /context:component-scan

    <!-- 默认的注解映射的支持 -->








    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">


    application/json;charset=UTF-8


    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">




  • web.xml
    <?xml version="1.0" encoding="UTF-8"?>



    index.jsp


    contextConfigLocation
    classpath*:spring-main.xml


    org.springframework.web.context.ContextLoaderListener


    springMVC
    org.springframework.web.servlet.DispatcherServlet

    contextConfigLocation
    classpath*:spring-servlet.xml

    1


    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter

    encoding
    UTF-8


    forceEncoding
    true



    encodingFilter
    /*


    springMVC
    .do


    hibernateFilter
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

    singleSession
    true


    sessionFactoryBeanName
    sessionFactory



    hibernateFilter
    .do


    Faces Servlet
    javax.faces.webapp.FacesServlet
    1


    Faces Servlet
    .faces


    AdminUserFilter
    com.demo.manage.action.AdminUserFilter


    AdminUserFilter
    .do

*项目结构如下
*测试controller
package com.demo.action;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.model.UserModel;
@Controller
@RequestMapping("test")
@Transactional
public class TestAction extends AbstractAction{
@RequestMapping("test")
@ResponseBody
@Transactional(rollbackFor=Exception.class)
public String test(int id) throws Exception{
String sql="update usertable set vip=2 where usersID=2";
entityPersist.writeBySql(sql);

    if(id==2){
        throw new RuntimeException("cuowu ");
    }
    return "ok";
}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐