消息关闭
    暂无新消息!
SSM搭建的框架,dao层使用mybatis的mapper代理,现在想通过@Transactional注解控制事务,数据无法回滚,事务不生效
现贴出代码,请大神帮我找找原因,应该怎么改

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>denCMS</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>XSSFilter</filter-name>
    <filter-class>com.dengx.filter.XSSFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>XSSFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/den/druid/*</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>druidWebStatFilter</filter-name>
    <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
    <init-param>
      <param-name>exclusions</param-name>
      <param-value>/public/*,*.js,*.css,/druid*,*.jsp,*.swf</param-value>
    </init-param>
    <init-param>
      <param-name>principalSessionName</param-name>
      <param-value>sessionInfo</param-value>
    </init-param>
    <init-param>
      <param-name>profileEnable</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>druidWebStatFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


2.springmvc.xml


<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->
<context:component-scan base-package="com.dengx.controllor"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>


<!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/"/>
<!-- 配置jsp路径的后缀 <property name="suffix" value=".jsp"/> -->
</bean>


<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>

<!-- 注解适配器  json转换器,因为配置的是自动注解  <mvc:annotation-driven></mvc:annotation-driven> 所以无需再配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean> -->
</beans>


3.applicationContext-dao.xml

<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 配置数据源 ,dbcp 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  init-method="init" destroy-method="close"> 
   <property name="driverClassName" value="${jdbc.driver}" /> 
   <property name="url" value="${jdbc.url}" /> 
   <property name="username" value="${jdbc.username}" /> 
   <property name="password" value="${jdbc.password}" /> 
   <!-- 配置初始化大小、最小、最大 --> 
   <property name="initialSize" value="1" /> 
   <property name="minIdle" value="1" /> 
   <property name="maxActive" value="10" />
   <!-- 配置获取连接等待超时的时间 --> 
   <property name="maxWait" value="10000" />
   <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
   <property name="timeBetweenEvictionRunsMillis" value="60000" />
   <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
   <property name="minEvictableIdleTimeMillis" value="300000" />
   <property name="testWhileIdle" value="true" />
   <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> 
   <property name="testOnBorrow" value="true" /> 
   <property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
   <property name="poolPreparedStatements" value="true" /> 
   <property name="maxPoolPreparedStatementPerConnectionSize"  value="20" />
   <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
   <property name="defaultAutoCommit" value="true" />
   <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> 
   <property name="validationQuery" value="select 1 " /> 
   <property name="filters" value="stat" /> 
   <property name="proxyFilters"> 
    <list> 
     <ref bean="logFilter" /> 
    </list> 
   </property> 
  </bean>
<bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> 
  <property name="statementExecutableSqlLogEnable" value="false" /> 
</bean>

<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" /> <!-- 数据库连接池 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> <!-- 加载mybatis的全局配置文件 -->
</bean>

<!-- mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
<property name="basePackage" value="com.dengx.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>


<!-- 加载数据字典缓存 -->
<bean id="myEhcacheManager" class="com.dengx.common.cache.MyEhcacheManager" >
  <constructor-arg ref="dataSource"></constructor-arg>   
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

4.applicationContext-service.xml


<!-- 用户的service -->
<bean id="userServiceImpl" class="com.dengx.service.impl.sys.T00_userServiceImpl"/>
<!-- 友情链接 -->
<bean id="friendLinkServiceImpl" class="com.dengx.service.impl.sys.FriendLinkServiceImpl"/>
<!-- 菜单资源 -->
<bean id="menusServiceImpl" class="com.dengx.service.impl.sys.MenusServiceImpl"/>
<!-- 数据字典加载 -->
<bean id="dictServiceImpl" class="com.dengx.service.impl.sys.DictServiceImpl"/>
<!-- 角色-->
<bean id="roleServiceImpl" class="com.dengx.service.impl.sys.RoleServiceImpl"/>
</beans>


5.applicationContext-transaction.xml

<!-- 事务管理器  对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 dataSource在applicationContext-dao.xml中配置了 -->
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--  aop中impl..*.*(..))为impl包以及子包里所以类的任意方法 ,impl.*.*(..))为仅impl包下类的任意方法   -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.dengx.service.impl..*.*(..))"/>
</aop:config>

</beans>


6.action

@Controller 
@RequestMapping("/den/role")
public class RoleAction extends BaseAction{

@Autowired
private RoleService roleServiceImpl;
@Autowired
private MenusService menusServiceImpl;

private String msn;


@Transactional
@RequestMapping(value={"/deleteRole"},method = { RequestMethod.POST,RequestMethod.GET })
public String deleteRole(Role role,RedirectAttributes attr){
String[] ids = role.getSelectid();
for (String id : ids) {
role.setId(id);
int row = roleServiceImpl.deleteRoleById(role);
if(row>0){
roleServiceImpl.deleteRole2MenuById(role);
}
}
msn="角色创建成功!";
attr.addAttribute("msn", Dengx.getMsn(msn));

return "redirect:roleList.htm";
}


4个回答

︿ 0
 public static void main(String[] args) {
   String a="\\u0031\\u0030\\u0030\\u591a\\u4e2a\\u004a\\u0061\\u0076\\u0061\\u0057\\u0065\\u0062\\u9879\\u76ee\\u6e90\\u7801\\u002b\\u89c6\\u9891\\u0020\\u53ea\\u8981\\u0031\\u0030\\u5143\\u0020\\u6beb\\u65e0\\u4fdd\\u7559\\u7ed9\\u4f60\\u0020\\u0020\\u0053\\u0070\\u0072\\u0069\\u006e\\u0067\\u0020\\u0068\\u0069\\u0062\\u0065\\u0072\\u006e\\u0061\\u0074\\u0065\\u0020\\u006d\\u0079\\u0062\\u0061\\u0074\\u0069\\u0073\\u6846\\u67b6\\u0020\\u000d\\u000a\\u5e73\\u5747\\u4e00\\u4e2a\\u4e00\\u6bdb\\u94b1\\u4e0d\\u5230\\u000d\\u000a\\u5fae\\u4fe1\\uff1a\\u006a\\u0061\\u0076\\u0061\\u006a\\u0064\\u0062\\u0063\\u000d\\u000a\\u5b98\\u7f51\\uff1a\\u0077\\u0077\\u0077\\u002e\\u006a\\u0061\\u0076\\u0061\\u0030\\u0039\\u0030\\u0039\\u002e\\u0063\\u006f\\u006d\\uff08\\u5b98\\u7f51\\u53ef\\u4ee5\\u0032\\u0034\\u5c0f\\u65f6\\u81ea\\u52a8\\u53d1\\u8d27\\u0020\\u652f\\u4ed8\\u5b9d\\u63a5\\u53e3\\uff09";
   System.out.println(decode(a));
  }
  public static String decode(String unicodeStr) {  
    if (unicodeStr == null) {  
        return null;  
    }  
    StringBuffer retBuf = new StringBuffer();  
    int maxLoop = unicodeStr.length();  
    for (int i = 0; i < maxLoop; i++) {  
        if (unicodeStr.charAt(i) == '\\') {  
            if ((i < maxLoop - 5)  
                    && ((unicodeStr.charAt(i + 1) == 'u') || (unicodeStr  
                            .charAt(i + 1) == 'U')))  
                try {  
                    retBuf.append((char) Integer.parseInt(  
                            unicodeStr.substring(i + 2, i + 6), 16));  
                    i += 5;  
                } catch (NumberFormatException localNumberFormatException) {  
                    retBuf.append(unicodeStr.charAt(i));  
                }  
            else  
                retBuf.append(unicodeStr.charAt(i));  
        } else {  
            retBuf.append(unicodeStr.charAt(i));  
        }  
    }  
    return retBuf.toString();  
}