您的位置:首页 > 编程语言 > Java开发

Spring-hibernate-事务处理详解及案例<六>

2015-12-09 20:08 761 查看
问题?Spring-hibernate-事务处理详解及案例<六>

在上一篇文章中已经说到了事务了,这一次做一个spring+hibernate+事务管理的案例

必须的包



首先看看事务:我们主要用到两种事务

spring事务管理器:







案例:

jdbc.properies配置文件:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/use_aop
jdbc.username=root
jdbc.password=root


持久化类:

package cn.itcast.sh.hibernate.spring.domain;

/**
* Person entity. @author MyEclipse Persistence Tools
*/

public class Person implements java.io.Serializable {

// Fields

private Long pid;
private String pname;
private String psex;

// Constructors

/** default constructor */
public Person() {
}

/** full constructor */
public Person(String pname, String psex) {
this.pname = pname;
this.psex = psex;
}

// Property accessors

public Long getPid() {
return this.pid;
}

public void setPid(Long pid) {
this.pid = pid;
}

public String getPname() {
return this.pname;
}

public void setPname(String pname) {
this.pname = pname;
}

public String getPsex() {
return this.psex;
}

public void setPsex(String psex) {
this.psex = psex;
}

}


Person.hbm.xml映射文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.itcast.sh.hibernate.spring.domain.Person" table="person">
<id name="pid" type="java.lang.Long">
<column name="pid" />
<generator class="native" />
</id>
<property name="pname" type="java.lang.String">
<column name="pname" length="20" />
</property>
<property name="psex" type="java.lang.String">
<column name="psex" length="5" />
</property>
</class>
</hibernate-mapping>


hibernate.cgf.xml配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--
一个session-factory只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">root</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/use_aop
</property>
<!--
作用:根据持久化类和映射文件生成表
validate
create-drop
create
update
-->
<property name="hbm2ddl.auto">update</property>
<!--
显示hibernate内部生成的sql语句
-->
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- 添加映射文件 -->
<mapping
resource="cn/itcast/sh/hibernate/spring/domain/Person.hbm.xml" />

</session-factory>
</hibernate-configuration>


dao层:

package cn.itcast.sh.hibernate.spring.dao;

import cn.itcast.sh.hibernate.spring.domain.Person;

public interface PersonDao {
public void savePerson(Person person);
}


package cn.itcast.sh.hibernate.spring.dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.itcast.sh.hibernate.spring.dao.PersonDao;
import cn.itcast.sh.hibernate.spring.domain.Person;

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

public void savePerson(Person person) {
this.getHibernateTemplate().save(person);

}
}


service层

package cn.itcast.sh.hibernate.spring.service;

import cn.itcast.sh.hibernate.spring.domain.Person;

public interface PersonService {
public void savePerson(Person person);

}


package cn.itcast.sh.hibernate.spring.service.impl;

import cn.itcast.sh.hibernate.spring.dao.PersonDao;
import cn.itcast.sh.hibernate.spring.domain.Person;
import cn.itcast.sh.hibernate.spring.service.PersonService;

public class PersonServiceImpl implements PersonService {
private PersonDao personDao;

public PersonDao getPersonDao() {
return personDao;
}

public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

public void savePerson(Person person) {
//		int i=1/0;
this.personDao.savePerson(person);
}

}


异常切面类

package cn.itcast.sh.hibernate.spring.exception;

public class MyException {
public void myException(Throwable ee){
System.out.println("出错了: "+ee);
}
}


spring的applicationContext.xml配置文件

<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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 引入配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<!-- 引入数据源DataSource -->
<bean id="mydataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- 引入sessionFactory,LocalSessionFactoryBean的父类中就可以得到dataSource -->
<!-- 第一种方式 -->
<!-- ory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mydataSource"/>
</property>
<property name="mappingResources"> hibernate映射文件
<list>
<value>cn/itcast/sh/hibernate/spring/domain/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties"> 方言
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean> -->

<!-- 第二种sessionfactory方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"><!-- hibernate配置文件 -->
<value>classpath:cn/itcast/sh/hibernate/spring/config/hibernate.cfg.xml</value>
</property>
</bean>

<!-- Dao层 -->
<bean id="personDao" class="cn.itcast.sh.hibernate.spring.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- sercice层 -->
<bean id="personService" class="cn.itcast.sh.hibernate.spring.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
<!-- 创建事务管理器  -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- 创建事务管理器的对像名必须和下面,下面的通知transaction-manager=""相同 -->
<!-- 通知:
1、告诉spring容器,采用什么样的方法来处理事务
(这里采取的是jdbc的DataSourceTransactionManager的方式,
当然也有hibernate的事务)
2、高数spring容器,采取什么样的事务策略
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!--
name:规定方法的
isolation:数据库隔离机制   默认值DEFAULT
propagation:传播机制(属性)默认值REQUIRED
所以这两个属性不用写,直接就是默认值
-->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"  read-only="false"/>
</tx:attributes>
</tx:advice>

<!-- 引入错误显示得切面 -->
<bean id="Exception" class="cn.itcast.sh.hibernate.spring.exception.MyException"></bean>

<!-- aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.sh.hibernate.spring.service.impl.PersonServiceImpl.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/><!-- 启动事务管理器 -->
<aop:aspect ref="Exception">
<aop:after-throwing method="myException" throwing="ee" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>
</beans>


测试类:

package cn.itcast.sh.hibernate.spring.test;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.sh.hibernate.spring.dao.PersonDao;
import cn.itcast.sh.hibernate.spring.domain.Person;
import cn.itcast.sh.hibernate.spring.service.PersonService;

public class HibernateTransactionTest {
public static ApplicationContext applicationContext;
static{
applicationContext = new ClassPathXmlApplicationContext("cn/itcast/sh/hibernate/spring/config/applicationContext.xml");
}

@Test
public void dataSource(){
DataSource dataSource = (DataSource)applicationContext.getBean("mydataSource");
System.out.println(dataSource);
}

@Test
public void sessionfactory(){
SessionFactory sFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
System.out.println("sessionFactory  "+sFactory);
}

@Test
public void personDao(){
PersonDao personDao = (PersonDao)applicationContext.getBean("personDao");
System.out.println("Dao层  "+personDao);
}

@Test
public void personService(){
PersonService personService =  (PersonService)applicationContext.getBean("personService");
System.out.println("Service层   "+personService);
Person person1 = new Person();
person1.setPname("223");
person1.setPsex("男");
personService.savePerson(person1);
}

}


当所有都配置好了,测试service对象,如果出现以下结果:说明是代理对象,以及配置成功,记住一定要写一层测一层。



因为代理对象(目标方法)是放在service层的,如果出现异常,统一交给service处理:



最后插入数据库处理:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: