炸娃程序猿
用jdbcTemplate吧这个才是事务实现类的好模板
慕哥5346237
老师讲过了。。。。。。。。
<?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>
慕姐3017211
controller层不负责处理业务,dao层处理事务不符合单一职责原则,不利于复用.所以将事务管理放在service层
thezhw
https://www.imooc.com/video/9330照着敲一遍。
慕雪6201052
掌握最基本的事务管理中的两种方式,@Transactional注解结合applicationContext.xml中的annotation:driven管理事务,AspectJ的xml的配置方式大致就不错了。
ay悠悠
注解优先,注解设置不回滚,XML设置回滚,回滚无效
Zhong1
代码贴出来,可能是事物回滚了
qq_MJX_0
虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。
qq_幽城城主_0
链接:http://pan.baidu.com/s/1i3jexRR 密码:yj5n
肖申克赫本
没有 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
常文标ytb
http://pan.baidu.com/s/1eRGMYci
链接下载解压,即可
Allard
helloJavaEE
MyEclipse中有这个功能,但是Eclipse中没有,好像需要什么插件
fuWillSon
拦截器拦截的是请求,AOP和拦截器是两个概念,事务的处理不会使用拦截器的吧,因为事务是在service层中才会发生,而拦截器只会去拦截URLWEB页面的那些请求
rainbow702
是啊 ,还有这个怎么配置模型
jaky0306
延时加载的核心就是代理,之所以能够延时加载是因为框架根据你的bean,生成了一个bean的代理,这个代理在你需要调用延时加载的数据时,他会调用方法去查询数据库,也就实现了延时加载
假象
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);
}
}已解决。都没人回复我 好失望。。。