用jdbcTemplate吧这个才是事务实现类的好模板
老师讲过了。。。。。。。。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--配置数据源c3p0-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置业务层实现类-->
<bean class="spring.demo2.AccountServiceImpl" id="accountService">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置DAO类-->
<bean class="spring.demo2.AccountDaoImpl" id="accountDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置业务层的代理-->
<bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
id="transactionProxyFactoryBean">
<!--目标对象-->
<property name="target" ref="accountService"/>
<!--注入事务管理器-->
<property name="transactionManager" ref="transactionManager"/>
<!--注入属性-->
<property name="transactionAttributes">
<props>
<!--
prop的格式:
PROPAGATION:事务传播行为
ISOLATIN:事务的隔离行为
readOnly:只读
-Exception:发生哪些异常回滚事务
+Exception:发生哪些异常事务不回滚
-->
<!--*代表类中的所有方法-->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
掌握最基本的事务管理中的两种方式,@Transactional注解结合applicationContext.xml中的annotation:driven管理事务,AspectJ的xml的配置方式大致就不错了。
注解优先,注解设置不回滚,XML设置回滚,回滚无效
代码贴出来,可能是事物回滚了
虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。
没有 C3P0 jar包,
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.mchange.v2.c3p0.ComboPooledDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext1.xml]; nested exception is java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
http://pan.baidu.com/s/1eRGMYci
链接下载解压,即可
MyEclipse中有这个功能,但是Eclipse中没有,好像需要什么插件
拦截器拦截的是请求,AOP和拦截器是两个概念,事务的处理不会使用拦截器的吧,因为事务是在service层中才会发生,而拦截器只会去拦截URLWEB页面的那些请求
是啊 ,还有这个怎么配置模型
applicationContext.xml
<tx:annotation-driven /> <context:component-scan base-package="com.code" />
dispatcher-servlet.xml
<context:component-scan base-package="com.code" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
package com.code.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.code.dao.TestDAO; import com.code.entity.Test; import com.code.service.TestService; @Service public class TestServiceImpl implements TestService{ @Resource private TestDAO testDAO; @Override @Transactional public void sql() { Test t1 = new Test(); t1.setId(1L); t1.setNum(800); Test t2 = new Test(); t2.setId(2L); t2.setNum(1200); testDAO.update(t1); int bug = 1/0; testDAO.update(t2); } }
已解决。都没人回复我 好失望。。。