详解Spring MVC事务配置

论坛 期权论坛 脚本     
niminba   2021-5-23 02:57   648   0

要了解事务配置的所有方法,请看一下《Spring事务配置的5种方法

本文介绍两种配置方法:

 一、XML,使用tx标签配置拦截器实现事务

二、Annotation方式

以下所使用环境为Spring4.0.3、Hibernate4.3.5

 一、 XML,使用tx标签配置拦截器实现事务

Entity类User.java,持久化类,对应数据库表user

package com.lei.demo.entity;

import javax.persistence.*;

@Entity(name="users")
public class Users {
  
  public Users(){
    super();
  }
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="id")
  private Integer id;
  
  @Column(name="user_name",length=32)
  private String user_name;
  
  @Column(name="age")
  private Integer age;
  
  @Column(name="nice_name",length=32)
  private String nice_name;
  
  //属性实现......

}

UserDAO.javar,表user的一些操作,其中属性sessionFactory应该由Spring注入,如下:

package com.lei.demo.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.lei.demo.entity.Users;

public class UsersDAO {
  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public List<Users> getAllUser(){
    String hsql="from users";
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hsql);
    
    return query.list();
  }
}

UserService.java,业务实现类,如下

package com.lei.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lei.demo.dao.*;

public class UserService {
  private UsersDAO userDao;
  
  public int userCount(){
    return userDao.getAllUser().size();
  }

  public UsersDAO getUserDao() {
    return userDao;
  }

  public void setUserDao(UsersDAO userDao) {
    this.userDao = userDao;
  }

}

首先看一下xml配置,spring-hibernate.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    ">

  <!-- Hibernate4 -->
  <!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
  <context:property-placeholder location="classpath:persistence-mysql.properties" />
  
  <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
      <list>
        <!-- 可以加多个包 -->
        <value>com.lei.demo.entity</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> 
      </props>
    </property>
  </bean>
  
  <!-- 数据库映射 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="${jdbc.driverClassName}" />
   <property name="url" value="${jdbc.url}" />
   <property name="username" value="${jdbc.user}" />
   <property name="password" value="${jdbc.pass}" />
  </bean>
  
  <!-- 配置Hibernate事务管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  
  <!-- 配置事务[Y_H	\HOH\[YOH\H	\HOH\[YH[YOH\H	\HOH\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\[˙[\
KJ
NCBBXX\S\[H]\\[BCBBXX\[\S\[\\[H\[BCBBCBBOB]B/#9y#]\[9."b9aiP[[[;+#/*yyd+ i9d#U\X."b9aiP[[[;b&z(j9#/*.+y`9"yy&d+ Bi:acy"yi&.*[[X[Y/cy"][[X[YK9d[[X[Yb&yc&/[[[
8'[[X[Yx'J{yo#/o*l9k9. B."l,y+j:`yk{n#9&)9ki.h9"y`9n+b{.g&i&i&+/c.
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP