Spring学习笔记(17)----使用Spring注解方式管理事务

来源:互联网 发布:网络维护做点什么 编辑:程序博客网 时间:2024/05/01 05:13

使用Spring+JDBC集成步骤如下:

 *配置数据源,例如:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    <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="123456"/><!-- 连接池启动时的初始值 --><property name="initialSize" value="1"/><!-- 连接池的最大值 --><property name="maxActive" value="100"/><!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --><property name="maxIdle" value="2"/><!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --><property name="minIdle" value="1"/> </bean>

 *配置事务,配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置有两种方式:注解方式和基于XML配置的方式

 

下面演示下使用Spring注解方式管理事务

首先在配置文件中配置Spring提供的事务管理器

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     <!-- 指定数据源 -->     <property name="dataSource" ref="dataSource"/>    </bean>

 由于会使用注解方式,因此我们要打开注解处理器,对注解进行解析

<tx:annotation-driven transaction-manager="txManager"/>

 

这样我们的配置文件配置完成,下面我们在Mysql中建立一张表,

create table users(                    id int(11) not null auto_increment,   username varchar(20) not null,        primary key (id)                   ) 

 

根据数据库,我们创建javabean

package com.szy.spring.bean;/** * @author  coolszy * @time    Dec 6, 2009 2:13:33 PM */public class User{private int id;private String username;public int getId(){return id;}public void setId(int id){this.id = id;}public String getUsername(){return username;}public void setUsername(String username){this.username = username;}}

 

然后创建DAO接口,在DAO中提供几个方法:

package com.szy.spring.dao;import java.util.List;import com.szy.spring.bean.User;public interface UserDAO{public void save(User user);public void update(User user);Public User  getUser(int id);public void delete(int id);public List<User> getAllUsers();}

 

实现这个接口

 

package com.szy.spring.dao.impl;import java.util.List;import com.szy.spring.bean.User;import com.szy.spring.service.UserService;/** * @author  coolszy * @time    Dec 6, 2009 2:19:22 PM */public class UserDAOImpl implements UserDAO{public void delete(int id){}public List<User> getAllUsers(){return null;}public User getUser(int id){}public void save(User user){}public void update(User user){}}

 

下面把这个类交给Spring管理

<bean id="userDAO" class="com.szy.spring.dao.impl.UserDAOImpl"/>

 由于要通过数据源对表进行操作,因此在DAO中添加数据源。

private DataSource dataSource;public void setDataSource(DataSource dataSource){this.dataSource = dataSource;}

 

然后在配置文件中进行配置

<bean id="userDAO" class="com.szy.spring.service.impl.UserDAOImpl"><property name="dataSource" ref="dataSource"/></bean>

 

这样我们就把数据源注入到类中。

在UserDAOImpl类中我们提供了dataSource,这样我们就可以对数据库进行操作,但是不推荐直接使用dataSource,建议使用JdbcTemplate

private JdbcTemplate jdbcTemplate;public void setDataSource(DataSource dataSource){//this.dataSource = dataSource;this.jdbcTemplate=new JdbcTemplate(dataSource);}

 下面我们使用jdbcTemplate对数据库进行增删改查,详细代码见附件。

package com.szy.spring.dao.impl;import java.util.List;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;import com.szy.spring.bean.User;import com.szy.spring.dao.UserDAO;/** * @author  coolszy * @time    Dec 6, 2009 2:19:22 PM */public class UserDAOImpl implements UserDAO{//private DataSource dataSource;private JdbcTemplate jdbcTemplate;public void setDataSource(DataSource dataSource){//this.dataSource = dataSource;this.jdbcTemplate=new JdbcTemplate(dataSource);}public void delete(int id){jdbcTemplate.update("delete from users where id=?", new Object[]{id},new int[]{java.sql.Types.INTEGER});}public List<User> getAllUsers(){return (List<User>)jdbcTemplate.query("select * from users", new UserRowMapper());}public User getUser(int id){return (User)jdbcTemplate.queryForObject("select * from users where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER}, new UserRowMapper());}public void save(User user){ jdbcTemplate.update("insert into users(username) values(?)", new Object[]{user.getUsername()},new int[]{java.sql.Types.VARCHAR});}public void update(User user){jdbcTemplate.update("update users set username=? where id=?", new Object[]{user.getUsername(),user.getId()},new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});}}

 编写测试代码,代码运行正常。

在我们实现的每个方法中如delete()方法,如果delete方法是这样

public void delete(int id){jdbcTemplate.update("delete from users where id=?", new Object[]{id},new int[]{java.sql.Types.INTEGER});jdbcTemplate.update("delete from users where id=?", new Object[]{id},new int[]{java.sql.Types.INTEGER});}

 这样每条语句都会在各自的事务中执行,并不能保证在同一使用中执行,为了保证在同一事务中执行,我们应使用Spring容器提供的声明事务,我们在UserDAOImpl 类上加入@Transactional,表示该类受Spring事务管理。如果该类中每个方法不需要事务管理,如getUser方法,则在该方法前加入

@Transactional(propagation=Propagation.NOT_SUPPORTED)

 

 

PS:在上面的配置文件中我们在配置文件中指明了驱动类等信息,如果我们想写在配置文件中要怎么配置能,首先我们编写配置文件,

driverClassName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://localhost\:3306/testusername=rootpassword=123456initialSize=1maxActive=100maxIdle=2minIdle=1

 然后Spring的配置文件需进行如下配置:

<context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    <property name="driverClassName" value="${driverClassName}"/>    <property name="url" value="${url}"/>    <property name="username" value="${username}"/>    <property name="password" value="${password}"/><property name="initialSize" value="${initialSize}"/><property name="maxActive" value="${maxActive}"/><property name="maxIdle" value="${maxIdle}"/><property name="minIdle" value="${minIdle}"/> </bean>

  这样就可以从属性文件中读取到配置信息。

 

  • 17_使用Spring注解方式管理事务.rar (5.2 MB)
  • 下载次数: 82
0 0
原创粉丝点击