关闭

Spring2.0中声明式事务管理(读spring2.0技术手册,林信良)

标签: springintegerattributesbeanuseraop
1836人阅读 评论(0) 收藏 举报
分类:

Spring2.0中声明式事务管理(<spring2.0技术手册>,林信良)

1.    mysql中创建表user

CREATE TABLE user (

    id INT(11) NOT NULL auto_increment PRIMARY KEY,

    name VARCHAR(100) NOT NULL default '',

    age INT

) Type = InnoDB;

2.    编写userDAO

public class UserDAO implements IUserDAO {

    private JdbcTemplate jdbcTemplate;

   

    public void setDataSource(DataSource dataSource) {

        jdbcTemplate = new JdbcTemplate(dataSource);

    }

   

    public void insert(User user) {

       String name = user.getName();

       int age = user.getAge().intValue();

      

       jdbcTemplate.update("INSERT INTO user (name,age) "

               + "VALUES('" + name + "'," + age + ")");

    }

 

    public User find(Integer id) {

        List rows = jdbcTemplate.queryForList(

          "SELECT * FROM user WHERE id=" + id.intValue());

       

        Iterator it = rows.iterator();

        if(it.hasNext()) {

            Map userMap = (Map) it.next();

 

            Integer i = new Integer(userMap.get("id").toString());

            String name = userMap.get("name").toString();

            Integer age =

                  new Integer(userMap.get("age").toString());

 

            User user = new User();

            user.setId(i);

            user.setName(name);

            user.setAge(age);

           

            return user;

        }

 

        return null;

    }

}

3.    配置文件

<?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:aop="http://www.springframework.org/schema/aop"

  xmlns:tx="http://www.springframework.org/schema/tx"

  xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

  http://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

    <bean id="dataSource"

          class="org.springframework.jdbc.datasource.DriverManagerDataSource"

          destroy-method="close"> 

        <property name="driverClassName"

                  value="com.mysql.jdbc.Driver"/>

        <property name="url"

                  value="jdbc:mysql://localhost:3306/demo"/>

        <property name="username" value="root"/>

        <property name="password" value="root"/>

    </bean>

   

    <bean id="transactionManager"

          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

   

    <bean id="userDAO"

          class="onlyfun.caterpillar.UserDAO">

        <property name="dataSource" ref="dataSource"/>

    </bean>

   

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

        <tx:attributes>

            <tx:method name="insert*" propagation="REQUIRED"/>

            <tx:method name="find*" read-only="true"/>

        </tx:attributes>

    </tx:advice>

 

    <aop:config>

        <aop:pointcut id="userDAOPointcut"

             expression="execution(* onlyfun.caterpillar.IUserDAO.*(..))"/>   //前面*表示任意返回类型,后面表示onlyfun.caterpillar.IUserDAO中的所有方法。

        <aop:advisor advice-ref="txAdvice" pointcut-ref="userDAOPointcut"/>

    </aop:config>

 

</beans>

4.    如果采用annotion 配置文件将会更简单:

package onlyfun.caterpillar;

 

import java.util.Iterator;

import java.util.List;

import java.util.Map;

 

import javax.sql.DataSource;

 

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

 

public class UserDAO implements IUserDAO {

    private JdbcTemplate jdbcTemplate;

   

    public void setDataSource(DataSource dataSource) {

        jdbcTemplate = new JdbcTemplate(dataSource);

    }

//在需要进行事务处理的方法前加上@Transactional标注。

    @Transactional(propagation = Propagation.REQUIRED)

    public void insert(User user) {

       String name = user.getName();

       int age = user.getAge().intValue();

      

       jdbcTemplate.update("INSERT INTO user (name,age) "

               + "VALUES('" + name + "'," + age + ")");

    }

   

    @Transactional(readOnly=true)

    public User find(Integer id) {

        List rows = jdbcTemplate.queryForList(

          "SELECT * FROM user WHERE id=" + id.intValue());

       

        Iterator it = rows.iterator();

        if(it.hasNext()) {

            Map userMap = (Map) it.next();

 

            Integer i = new Integer(userMap.get("id").toString());

            String name = userMap.get("name").toString();

            Integer age =

                  new Integer(userMap.get("age").toString());

 

            User user = new User();

            user.setId(i);

            user.setName(name);

            user.setAge(age);

           

            return user;

        }

 

        return null;

    }

}

配置文件变得很简单:

只要加上

    <bean id="dataSource"

          class="org.springframework.jdbc.datasource.DriverManagerDataSource"

          destroy-method="close"> 

        <property name="driverClassName"

                  value="com.mysql.jdbc.Driver"/>

        <property name="url"

                  value="jdbc:mysql://localhost:3306/demo"/>

        <property name="username" value="root"/>

        <property name="password" value="root"/>

    </bean>

   

    <bean id="transactionManager"

          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

   

    <bean id="userDAO"

          class="onlyfun.caterpillar.UserDAO">

        <property name="dataSource" ref="dataSource"/>

    </bean>

   <!-- 

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

        <tx:attributes>

            <tx:method name="insert*" propagation="REQUIRED"/>

            <tx:method name="find*" read-only="true"/>

        </tx:attributes>

    </tx:advice>

 

    <aop:config>

        <aop:pointcut id="userDAOPointcut"

             expression="execution(* onlyfun.caterpillar.IUserDAO.*(..))"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="userDAOPointcut"/>

    </aop:config>

-->

<!-- tx:advice通过方法前的@Transactional已经表示出来,而后面pointcut已经隐含在里面,写在哪个包中的哪个类中哪个方法前. -->

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

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1065226次
    • 积分:12692
    • 等级:
    • 排名:第1078名
    • 原创:241篇
    • 转载:156篇
    • 译文:0篇
    • 评论:227条
    文章分类
    最新评论