关于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 {
@Override
public 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;
}
@Override
public 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>