spring与Hibernate的整合(以及spring的声明式事务处理)
来源:互联网 发布:p2p理财返利平台源码 编辑:程序博客网 时间:2024/06/03 09:01
关于spring的事务处理以及与Hibernate的整合:
1)创建一个实体类的对象
public class Person implements Serializable {private Long pid;private String name;private String description;
<pre name="code" class="html">
<span style="white-space:pre"></span>//setter and getter
}
<strong> <span style="color:#ff0000;"> 2)创建一个实体类的配置文件:Peron.hbm.xml文件</span></strong>
<span style="white-space:pre"></span><pre name="code" class="html"><?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"><hibernate-mapping><class name="com.itheima.spring.hibernate.domain.Person" table="person"><id name="pid" column="pid"><generator class="increment"></generator></id><property name="name" column="name" length="50"></property><property name="description" column="description" length="100"></property></class></hibernate-mapping>3)创建dao层;
<span style="white-space:pre"></span><pre name="code" class="html">public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {@Overridepublic void savePerson(Person person) {this.getHibernateTemplate().save(person);}}
<span style="white-space:pre"></span>该dao实现类需要继承<span style="font-family: Arial, Helvetica, sans-serif;">HibernateDaoSupport。方便后续的在spring的配置文件中注入sessionfactory对象</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span> </span>4)创建service层
<span style="white-space:pre"></span>注入dao层对象
<span style="white-space: pre;"></span><pre name="code" class="html">public class PersonServiceImpl implements PersonService {private PersonDao personDao;public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}@Overridepublic void savePerson(Person person) {this.personDao.savePerson(person);}}
<strong><span style="color:#ff0000;">5)配置响应的spring文件:</span></strong>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"<span style="white-space:pre"></span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<span style="white-space:pre"></span>xmlns:mvc="http://www.springframework.org/schema/mvc"<span style="white-space:pre"></span>xmlns:context="http://www.springframework.org/schema/context"<span style="white-space:pre"></span>xmlns:aop="http://www.springframework.org/schema/aop" <span style="white-space:pre"></span>xmlns:tx="http://www.springframework.org/schema/tx"<span style="white-space:pre"></span>xsi:schemaLocation="http://www.springframework.org/schema/beans <span style="white-space:pre"></span>http://www.springframework.org/schema/beans/spring-beans-3.2.xsd <span style="white-space:pre"></span>http://www.springframework.org/schema/mvc <span style="white-space:pre"></span>http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd <span style="white-space:pre"></span>http://www.springframework.org/schema/context <span style="white-space:pre"></span>http://www.springframework.org/schema/context/spring-context-3.2.xsd<span style="white-space:pre"></span>http://www.springframework.org/schema/aop <span style="white-space:pre"></span>http://www.springframework.org/schema/aop/spring-aop-3.2.xsd <span style="white-space:pre"></span>http://www.springframework.org/schema/tx <span style="white-space:pre"></span>http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><span style="white-space:pre"></span><!-- 数据源的配置 --><span style="white-space:pre"></span><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <span style="white-space:pre"></span><property name="locations" value="classpath:jdbc.properties"/><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><bean id="dataSource" <span style="white-space:pre"></span> class="org.springframework.jdbc.datasource.DriverManagerDataSource"><span style="white-space:pre"></span> <property name="driverClassName" value="${jdbc.driverClassName}"/><span style="white-space:pre"></span> <property name="url" value="${jdbc.url}"/><span style="white-space:pre"></span> <property name="username" value="${jdbc.username}"/><span style="white-space:pre"></span> <property name="password" value="${jdbc.password}"/><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><span style="white-space:pre"></span><property name="dataSource" ref="dataSource"></property><span style="white-space:pre"></span><property name="mappingDirectoryLocations"><span style="white-space:pre"></span><list><span style="white-space:pre"></span><!-- <span style="white-space:pre"></span>spring容器会去该包及子包下搜索所有的映射文件<span style="white-space:pre"></span> --><span style="white-space:pre"></span><value>com/itheima/spring/hibernate/domain</value><span style="white-space:pre"></span></list><span style="white-space:pre"></span></property><span style="white-space:pre"></span><property name="hibernateProperties"><span style="white-space:pre"></span><props><span style="white-space:pre"></span><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><span style="white-space:pre"></span><prop key="hibernate.show_sql">true</prop><span style="white-space:pre"></span><prop key="hibernate.hbm2ddl.auto">update</prop><span style="white-space:pre"></span></props><span style="white-space:pre"></span></property><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><!-- dao --><span style="white-space:pre"></span><bean id="personDao" class="com.itheima.spring.hibernate.dao.impl.PersonDaoImpl"><span style="white-space:pre"></span><!-- <property name="hibernateTemplate" ref="hibernateTemplate"></property>--><span style="white-space:pre"></span><property name="sessionFactory" ref="sessionFactory"></property><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><!-- <span style="white-space:pre"></span><bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"><span style="white-space:pre"></span> <property name="sessionFactory" ref="sessionFactory" /><span style="white-space:pre"></span> </bean><span style="white-space:pre"></span> --><span style="white-space:pre"></span><!-- service--><span style="white-space:pre"></span><bean id="personService" class="com.itheima.spring.hibernate.service.Impl.PersonServiceImpl"><span style="white-space:pre"></span><!-- 注入dao --><span style="white-space:pre"></span><property name="personDao"><span style="white-space:pre"></span><ref bean="personDao"/><span style="white-space:pre"></span></property><span style="white-space:pre"></span></bean>
6)测试sessionFactory对象;
public class SessionFactoryTest {
@Test
public void test_sessionFactroy(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
SessionFactory factory=(SessionFactory)context.getBean("sessionFactory");
System.out.println(factory);
}
@Test
public void test_sessionFactroy(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
SessionFactory factory=(SessionFactory)context.getBean("sessionFactory");
System.out.println(factory);
}
<span style="white-space:pre"></span>测试成功后会得到sessionfactory对象
<span style="white-space: pre;"><strong><span style="color:#ff0000;">7)使用spring AOP进行声明式的事务配置</span></strong></span>
<span style="white-space: pre;"><strong></strong></span>
<span style="white-space: pre;"><!-- aop的配置 <span style="white-space: pre;"></span>将事务的处理交给spring处理;<span style="white-space: pre;"></span>--><span style="white-space: pre;"></span><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><span style="white-space: pre;"></span><!-- --><span style="white-space: pre;"></span><property name="sessionFactory"><span style="white-space: pre;"></span><ref bean="sessionFactory"/><span style="white-space: pre;"></span></property><span style="white-space: pre;"></span></bean><span style="white-space: pre;"></span><tx:advice transaction-manager="transactionManager" id="tx"><span style="white-space: pre;"></span><tx:attributes><span style="white-space: pre;"></span><tx:method name="save*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/><span style="white-space: pre;"></span></tx:attributes><span style="white-space: pre;"></span></tx:advice><span style="white-space: pre;"></span><span style="white-space: pre;"></span><aop:config><span style="white-space: pre;"></span><!-- 配置切入点 --><span style="white-space: pre;"></span><aop:pointcut expression="execution(* com.itheima.spring.hibernate.service.Impl.*.*(..))" id="perform"/><span style="white-space: pre;"></span><span style="white-space: pre;"></span><aop:advisor advice-ref="tx" pointcut-ref="perform"/><span style="white-space: pre;"></span></aop:config></beans><strong></strong></span>
<span style="white-space: pre;"><strong></strong></span>
<strong><span style="color:#ff0000;">8)测试:</span></strong>
<span style="font-weight: bold; white-space: pre;"></span>@Test<span style="white-space: pre;"></span>public void test_transactionManager(){<span style="white-space: pre;"></span>ApplicationContext context=<span style="white-space: pre;"></span>new ClassPathXmlApplicationContext("applicationContext.xml");<span style="white-space: pre;"></span>PersonDao dao=(PersonDao)context.getBean("personDao");<span style="white-space: pre;"></span>PersonService personService=(PersonService)context.getBean("personService");<span style="white-space: pre;"></span><span style="white-space: pre;"></span>System.out.println(personService);<span style="white-space: pre;"></span>Person person=new Person();<span style="white-space: pre;"></span>person.setName("张三");<span style="white-space: pre;"></span>person.setDescription("这是一个帅哥啊");<span style="white-space: pre;"></span><span style="white-space: pre;"></span>personService.savePerson(person);<span style="white-space: pre;"></span><span style="white-space: pre;"></span><span style="white-space: pre;"></span>}<strong></strong>
<span style="white-space: pre;"><strong></strong></span>
1 0
- spring与Hibernate的整合(以及spring的声明式事务处理)
- Spring的事务处理机制 ---以及spring整合hibernate事务管理配置
- Spring 的声明式事务处理
- Spring的声明式事务处理
- Spring的事务处理之声明式事务处理
- spring整合hibernate的声明式事务管理
- Spring整合Hibernate 声明式的事务管理
- spring声明式事务处理(hibernate)
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- Spring与Struts2以及Hibernate的整合
- 【spring 7】spring和Hibernate的整合:声明式事务
- Spring学习心得(23)-- spring声明式事务处理(hibernate)
- Spring——声明式的事务处理
- 整合spring 支持声明式事务的hibernate
- Spring 整合 Hibernate、声明式的事务管理、HibernateTemplate(HibernateDaoSupport)
- Spring与Hibernate的整合
- hibernate与spring的整合
- js中的KeyCode码
- 1009. Triple Inversions (35)
- 汇编语言将回车键的问题和集成开发的问题
- linux命令集锦之curl
- PAT乙级 打印沙漏(20)
- spring与Hibernate的整合(以及spring的声明式事务处理)
- 任意多边形面积
- 动态设置Layout的宽高值
- ansible高级用法
- 模块化程序设计函数与方法
- 一个类似CColorDialog中颜色选择的控件实现
- 10024---CSS Align(对齐)
- 浅析人脸检测之Haar分类器方法
- ASP.NET MVC中几个运用技巧