• Spring+Hibernate配置事务


    XML配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      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-2.0.xsd">
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
    <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </property>
    <property name="url">
    <value>jdbc:microsoft:sqlserver://localhost:1433</value>
    </property>
    <property name="username">
    <value>sa</value>
    </property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
              org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>beans/Users.hbm.xml</value>
    </list>
    </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
    <bean id="UsersDAO" class="beans.UsersDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager"/>
    </property>
    <!-- 指定直接对类进行代理,将属性proxyTargetClass指定为true(默认是false) -->
    <property name="proxyTargetClass" value="true"/>
    <property name="target">
    <ref local="UsersDAO"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <!-- key表示匹配DAO中的方法名,例如:save*表示所有前缀为save的方法 -->
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    </beans>
    <!--
    PROPAGATION_REQUIRED 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
    PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。 
    PROPAGATION_MANDATORY 支持当前事务,如果当前没有事务,就抛出异常。 
    PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。 
    PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
    PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。 
    PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。 
    -->

    UsersDAO

    package beans;

    import java.util.List;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.marker.spring.MyHibernateDao;
    import org.springframework.context.ApplicationContext;

    public class UsersDAO extends MyHibernateDao implements UsersInterface
    ...{
    private static final Log log = LogFactory.getLog(UsersDAO.class);
    public static final String NAME = "name";
    public static final String PWD = "pwd";
    public void save(Users transientInstance)
    ...{
            log.debug("saving Users instance");
    try
    ...{
                getHibernateTemplate().save(transientInstance);
                log.debug("save successful");
            }
    catch (RuntimeException re)
    ...{
                log.error("save failed", re);
    throw re;
            }
        }
    public void delete(Users persistentInstance)
    ...{
            log.debug("deleting Users instance");
    try
    ...{
                getHibernateTemplate().delete(persistentInstance);
                log.debug("delete successful");
            }
    catch (RuntimeException re)
    ...{
                log.error("delete failed", re);
    throw re;
            }
        }
    public Users findById(java.lang.Integer id)
    ...{
            log.debug("getting Users instance with id: " + id);
    try
    ...{
                Users instance = (Users) getHibernateTemplate().get("beans.Users",
                        id);
    return instance;
            }
    catch (RuntimeException re)
    ...{
                log.error("get failed", re);
    throw re;
            }
        }
    public void update(Users user)
    ...{
    try
    ...{
                getHibernateTemplate().update(user);
                log.debug("update successful");
            }
    catch (RuntimeException re)
    ...{
                log.error("update failed",re);
            }
        }
    }

    UsersInterface

    package beans;

    public interface UsersInterface
    ...{
    void update(Users user);
    }

    JUnit单元测试

    package test;

    import junit.framework.TestCase;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import beans.Users;
    import beans.UsersDAO;

    public class UsersDAOTest extends TestCase
    ...{
    private ApplicationContext context;
    private UsersDAO dao;
    protected void setUp() throws Exception
    ...{
            context = new ClassPathXmlApplicationContext("applicationContext.xml");
            dao = (UsersDAO) context.getBean("userDAOProxy");
            System.out.println("%%%%% 单元测试开始 %%%%%");
        }
    protected void tearDown() throws Exception
    ...{
            System.out.println("%%%%% 单元测试结束 %%%%%");
        }
    public void testSave()
    ...{
            Users user = new Users();
            user.setName("777");
            user.setPwd("888");
            dao.save(user);
        }
    public void testDelete()
    ...{
            Users user = new Users();
            user.setId(28);
            dao.delete(user);
        }
    public void testFindById()
    ...{
            Users user = dao.findById(26);
            assertNull(user.getName(), user.getId());
        }
    public void testUpdate()
    ...{
            Users user = new Users();
            user.setId(22);
            user.setName("654321");
            user.setPwd("999999");
            dao.update(user);
        }
    }

  • 相关阅读:
    转:无监督特征学习——Unsupervised feature learning and deep learning
    转:RESTful架构详解
    转:TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
    转:利用深度学习方法进行情感分析以及在海航舆情云平台的实践
    利用深度学习方法进行情感分析以及在海航舆情云平台的实践
    转:【AI每日播报】从TensorFlow到Theano:横向对比七大深度学习框架
    三分天注定,七分靠打拼,爱拼才会赢!
    我与小娜(36):人机大战第五局,AlphaGo必胜!
    最简单的基于FFmpeg的移动端样例:IOS 视频解码器
    3D数学读书笔记——矩阵基础番外篇之线性变换
  • 原文地址:https://www.cnblogs.com/yanzi629/p/2854896.html
Copyright © 2020-2023  润新知