使用注解方式配置spring中的jdbc事务
来源:互联网 发布:初中物理辅导软件 编辑:程序博客网 时间:2024/06/05 05:57
jdbc事务管理,使用注解的方式步骤如下
* 引入命名空间
* xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
* xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
--事务的命名空间
* xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
* 在beans.xml文件中增加如下配置
<!--1 增加spring的自动扫描功能 -->
<context:component-scan base-package="cn.itcast" />
<!-- 2 配置dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!--配置初始化连接数 -->
<property name="initialSize" value="5"/>
<!-- 配置最大连接数 -->
<property name="maxActive" value="20"/>
<!-- 配置最大空闲数 防止洪峰退去时,连接池中连接数过多-->
<property name="maxIdle" value="10"/>
<!-- 配置最小空闲数 防止洪峰到来时,连接池中连接池中连接的数量过少,引起的性能开销-->
<property name="minIdle" value="5"/>
<!--设置最大等待时间,如果超过这个时间,连接池将抛出异常-->
<property name="maxWait" value="5000"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!--3 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4 配置jdbc的事务管理器 Aop的切面-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 5 配置事务级别的注解使用的事务管理器 -->
<tx:annotation-driven transaction-manager="txManager"/>
* 处理dao层
* 处理存款的dao层
* 创建InAccountDaoImpl对象 使用@Repository注解
@Repository("inAccountDao")
public class InAccountDaoImpl implements InAccountDao
* 注入jdbcTemplate到该dao层
@Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
* 处理账户的dao层
* 创建AccountDaoImpl对象 使用@Repository注解
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao
* 注入jdbcTemplate到该dao层
@Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
* 处理业务层
* 创建业务层的对象
@Service("inAccountService")
public class InAccountServiceImpl implements InAccountService
* 注入dao层的对象
@Resource(name="inAccountDao")
private InAccountDao inAccountDao;
@Resource(name="accountDao")
private AccountDao accountDao;
* 处理事务
* 定义类级别的事务(设置事务时只读的)
@Transactional(readOnly=true)
@Service("inAccountService")
public class InAccountServiceImpl implements InAccountService
* 定义方法级别的事务
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false)
public void saveInAccount(InAccount inAccount)
注:方法级别的事务会覆盖类级别的事务
* 测试
* 引入命名空间
* xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
* xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
--事务的命名空间
* xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
* 在beans.xml文件中增加如下配置
<!--1 增加spring的自动扫描功能 -->
<context:component-scan base-package="cn.itcast" />
<!-- 2 配置dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!--配置初始化连接数 -->
<property name="initialSize" value="5"/>
<!-- 配置最大连接数 -->
<property name="maxActive" value="20"/>
<!-- 配置最大空闲数 防止洪峰退去时,连接池中连接数过多-->
<property name="maxIdle" value="10"/>
<!-- 配置最小空闲数 防止洪峰到来时,连接池中连接池中连接的数量过少,引起的性能开销-->
<property name="minIdle" value="5"/>
<!--设置最大等待时间,如果超过这个时间,连接池将抛出异常-->
<property name="maxWait" value="5000"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!--3 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4 配置jdbc的事务管理器 Aop的切面-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 5 配置事务级别的注解使用的事务管理器 -->
<tx:annotation-driven transaction-manager="txManager"/>
* 处理dao层
* 处理存款的dao层
* 创建InAccountDaoImpl对象 使用@Repository注解
@Repository("inAccountDao")
public class InAccountDaoImpl implements InAccountDao
* 注入jdbcTemplate到该dao层
@Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
* 处理账户的dao层
* 创建AccountDaoImpl对象 使用@Repository注解
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao
* 注入jdbcTemplate到该dao层
@Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
* 处理业务层
* 创建业务层的对象
@Service("inAccountService")
public class InAccountServiceImpl implements InAccountService
* 注入dao层的对象
@Resource(name="inAccountDao")
private InAccountDao inAccountDao;
@Resource(name="accountDao")
private AccountDao accountDao;
* 处理事务
* 定义类级别的事务(设置事务时只读的)
@Transactional(readOnly=true)
@Service("inAccountService")
public class InAccountServiceImpl implements InAccountService
* 定义方法级别的事务
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false)
public void saveInAccount(InAccount inAccount)
注:方法级别的事务会覆盖类级别的事务
* 测试
- 使用注解方式配置spring中的jdbc事务
- 【Spring】在Spring框架下使用注解配置JDBC事务
- Spring JDBC-使用注解配置声明式事务
- spring学习笔记10--Spring+jdbc组合开发(注解方式配置事务)
- Spring 采用注解方式配置事务
- spring配置事务之注解方式
- Spring采用注解方式配置事务
- spring配置声明式事务--注解方式
- Spring配置声明式事务--注解方式
- Spring 事务基于注解方式的配置
- spring jdbc 事务配置----TransactionTemplate使用
- 在spring中使用JDBC事务配置
- Spring事务管理-使用注解配置事务
- Spring事务管理-使用注解配置事务
- 使用Spring中的 @Transactional注解控制事务
- spring配置JDBC事务
- spring(JDBC)事务配置
- Spring注解事务配置
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决
- VB基本知识
- boost::noncopyable
- poj1276 Cash Machine
- SNMP++在vs2008下的编译
- 使用注解方式配置spring中的jdbc事务
- Android之网络操作 - 从网络获取图片或网页
- WebLogic启动取消用户及密码输入
- freeswitch 防火墙 端口整理
- ……………………………分割线…………………………………………
- C/C++指针小结
- mysql2 及path配置
- net网页开发中的三层架构
- 探索 Java 同步机制