springmvc+mybatis+mysql事物失效是怎么回事

Alanzhou007 2014-09-11 10:59:38
spring-mybatis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- 启动spring注解,当自动扫描启动后,该配置可以去掉 -->
<context:annotation-config />
<context:component-scan base-package="com.house365.service.impl"/>

<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/mybatis/jdbc.properties" />
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}"
p:acquireIncrement="10" p:idleConnectionTestPeriod="60" p:maxPoolSize="100"
p:maxStatements="50" p:minPoolSize="10" />

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />

<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />

<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.house365.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="WEB-INF/mybatis/mybatis-config.xml" />
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.house365.dao" />
</bean>
</beans>


===========================================================================================
springmvc-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.house365.controller" />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<import resource="spring-mybatis.xml"/>
</beans>
========================================================================================

Controller:

@Controller
public class UserComment {
//@Resource(name = "usercommentService")
@Autowired
private UserCommentService usercommentservice;

@RequestMapping("/hellcomment")
public String hellcomment(ModelMap mp)
{
List<Comment> list = usercommentservice.queryCommentAll();
mp.addAttribute("users", list);
return "comment";

}

@RequestMapping(value = "/addcomment")
public ModelAndView addcomment(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView("hello");
String userName = request.getParameter("username");
String passWord = request.getParameter("password");
//测试事物是否回滚
usercommentservice.addUserComent();
usercommentservice.addUser();

return mv;
}
}


================================================================================================
service层:
@Service
@Transactional(rollbackFor=Exception.class)
public class UserCommentImpl implements UserCommentService{

//@Resource(name = "usercommentDao")
@Autowired
private UserCommentDao usercommentDao;
@Override
public List<Comment> queryCommentAll() {
return usercommentDao.queryCommentAll();
}
@Override
public void addUserComent() {
usercommentDao.addUserComent();
}
@Override
public void addUser() {

usercommentDao.addUser();
}

}



...全文
114 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Alanzhou007 2014-09-11
  • 打赏
  • 举报
回复
没人回吗
jnu_forum是基于Java的论坛系统。前端使用Html+CSS+JS实现,后端使用Java语言开发,技术栈包括但不限于Spring/SpringMVC/SpringBoot、MyBatis、Redis、PageHelper、MySQL、Maven等,开发工具为Eclipse。 功能 1、登录和注册 2、(分类)浏览话题 3、发表话题 4、上传照片 5、评论以及评论赞踩 6、站内信通知 7、用户积分排行榜 8、关注和共同关注 主要功能实现 1、登录注册:使用SpringSecurity4框架,即使用已经包装好的接口来实现,简单易用。 2、上传照片:照片是存储在第三方服务器,即七牛云。 3、站内信通知:通过异步队列来实现的站内信通知,其中选择Redis来作为队列。 4、排行榜:排行榜是通过Redis的有序集合来实现的,可以快速实现topK排序。 5、关注和共同关注:通过Redis的集合数据结构实现。 页面展示 1、首页 2、注册 3、登录 4、话题浏览页面 5、话题详情页面与评论 6、个人简介页面 7、发表话题页面 8、站内信页面 9、照片墙页面 10、关注和粉丝界面 10、排行榜界面 备注: 1、本项目的Redis已经换成集群了,本地跑的时候先建立集群,否则自行将集群换成单机Redis,具体修改application.propertie和com.xzp.forum.util.JedisAdapter.java即可(再具体如何修改可以参考提交记录或联系我~) 2、因项目中七牛云过期了,上传的所有照片都失效了,所以项目中有照片的都被和谐了

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧