spring2.5整合hibernate3.0

第一步:配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context" 
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12 
13      <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
14      <context:annotation-config/>
15      <!-- 配置dataSource -->
16      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
17         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
18         <property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
19         <property name="username" value="tsrescue"/>
20         <property name="password" value="123456"/>
21          <!-- 连接池启动时的初始值 -->
22          <property name="initialSize" value="1"/>
23          <!-- 连接池的最大值 -->
24          <property name="maxActive" value="500"/>
25          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
26          <property name="maxIdle" value="2"/>
27          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
28          <property name="minIdle" value="1"/>
29      </bean>
30      <!-- 配置sessionFactory -->
31      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
32          <property name="dataSource" ref="dataSource"/>
33          <property name="mappingResources">
34             <list>
35               <value>cn/itcast/bean/Person.hbm.xml</value>
36             </list>
37          </property>
38          <property name="hibernateProperties">
39              <props>
40                 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
41                 <prop key="hibernate.show_sql">true</prop>
42                 <prop key="hibernate.format_sql">true</prop>
43                 <prop key="hibernate.current_session_context_class">thread</prop>这个配置不需要!!!!!!!!!!!!!!
44             </props>
45            
46          </property>
47     </bean>
48     <!-- spring提供的针对hibernate的事务管理器 -->
49     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
50           <property name="sessionFactory" ref="sessionFactory"/>
51     </bean>
52     <!-- 基于注解的方式声明事务 -->
53     <tx:annotation-driven transaction-manager="txManager"/>
54     <bean id = "personService" class="cn.itcast.Service.Impl.PersonServiceBean"/>
55 </beans>

代码部分:

1.person.java

 1 package cn.itcast.bean;
 2 
 3 public class Person {
 4     private Integer id;
 5     private String name;
 6     
 7     public Person() {}
 8 
 9     public Person(String name) {
10         this.name = name;
11     }
12     
13     public Integer getId() {
14         return id;
15     }
16     public void setId(Integer id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     
26     
27     
28 }

2.Person.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2016-6-1 10:08:44 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping package="cn.itcast.bean">
 6     <class name="Person" table="person">
 7         <id name="id">
 8             <generator class="native" />
 9         </id>
10         <property name="name"/>
11     </class>
12 </hibernate-mapping>

3.PersonService 接口

 1 package cn.itcast.Service;
 2 
 3 import java.util.List;
 4 
 5 import cn.itcast.bean.Person;
 6 
 7 public interface PersonService {
 8 
 9     public abstract void save(Person person);
10 
11     public abstract void update(Person person);
12 
13     public abstract Person getPerson(Integer personId);
14 
15     public abstract void delete(Integer personId);
16 
17     public abstract List<Person> getPersons();
18 
19 }

4.PersonService接口的实现类PersonServiceBean 

 1 package cn.itcast.Service.Impl;
 2 
 3 import java.util.List;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.hibernate.Session;
 8 import org.hibernate.SessionFactory;
 9 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional;
11 
12 import cn.itcast.Service.PersonService;
13 import cn.itcast.bean.Person;
14 @Transactional
15 public class PersonServiceBean implements PersonService {
16     @Resource
17     private SessionFactory sessionFactory;
18     
19     @Override
20     public void save(Person person){
21         //取被spring容器管理的session
22         sessionFactory.getCurrentSession().persist(person);
23     }
24     
25     @Override
26     public void update(Person person){
27         sessionFactory.getCurrentSession().merge(person);
28     }
29     
30     @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
31     @Override
32     public Person getPerson(Integer personId){
33         return (Person) sessionFactory.getCurrentSession().get(Person.class, personId);
34         
35     }
36     
37     @Override
38     public void delete(Integer personId){
39         sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId));
40     }
41     
42     @Transactional(propagation =Propagation.NOT_SUPPORTED,readOnly = true)
43     @SuppressWarnings("unchecked")
44     @Override
45     public List<Person> getPersons(){
46         return sessionFactory.getCurrentSession().createQuery("from person").list();
47         
48     }
49 }

5.测试类

 1 package cn.itcast.Service.Impl;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import java.util.List;
 6 
 7 import org.junit.BeforeClass;
 8 import org.junit.Test;
 9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11 
12 import cn.itcast.Service.PersonService;
13 import cn.itcast.bean.Person;
14 
15 public class PersonServiceBeanTest {
16     private static PersonService ps;
17     @BeforeClass
18     public static void setUpBeforeClass() throws Exception {
19         try {
20             ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
21             ps = (PersonService) ctx.getBean("personService");
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }
26 
27     @Test
28     public void testSave() {
29         ps.save(new Person("小明3"));
30     }
31 
32     @Test
33     public void testUpdate() {
34         Person person = ps.getPerson(1);
35         person.setName("小明修改版");
36         ps.update(person);
37     }
38 
39     @Test
40     public void testGetPerson() {
41         Person p = ps.getPerson(1);
42         System.out.println(p.getName());
43     }
44 
45     @Test
46     public void testDelete() {
47         ps.delete(2);
48     }
49 
50     @Test
51     public void testGetPersons() {
52         List<Person> list = ps.getPersons();
53         for (Person person : list) {
54             person.getName();
55         }
56     }
57 
58 }
原文地址:https://www.cnblogs.com/jiangjianzhu/p/5714458.html