2012年3月15日星期四

spring开发_Spring+Hibernate_HibernateDaoSupport

原有项目:

http://www.cnblogs.com/hongten/archive/2012/03/15/java_spring_hibernate_hibernateTemplate.html

修改类:

/spring+hibernate_hibernateDaoSupport/src/com/b510/service/impl/PersonServiceBean.java

 1 package com.b510.service.impl;
2
3 import java.util.List;
4
5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
6 import org.springframework.transaction.annotation.Propagation;
7 import org.springframework.transaction.annotation.Transactional;
8
9 import com.b510.domain.Person;
10 import com.b510.service.PersonService;
11
12 /**
13 * 使用注解方式进行事务管理
14 *
15 * @author Hongten
16 *
17 */
18 public class PersonServiceBean extends HibernateDaoSupport implements
19 PersonService {
20
21 @Override
22 public void delete(Integer id) {
23 getHibernateTemplate().delete(getPerson(id));
24 }
25
26 // 在查询的时候,不需要开启事务,并且指定为只读,这样可以提高查询效率
27 @Override
28 @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
29 public Person getPerson(Integer id) {
30 return (Person) getHibernateTemplate().get(Person.class, id);
31 }
32
33 // 在查询的时候,不需要开启事务,并且指定为只读,这样可以提高查询效率
34 @Override
35 @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
36 @SuppressWarnings("unchecked")
37 public List<Person> getPerson() {
38 return getHibernateTemplate().find("from Person");
39 }
40
41 @Override
42 public void save(Person person) {
43 getHibernateTemplate().save(person);
44 }
45
46 @Override
47 public void update(Person person) {
48 getHibernateTemplate().update(person);
49 }
50 }

/spring+hibernate_hibernateDaoSupport/src/bean.

修改为这样:

1     <bean id="personService" >
2 <property name="sessionFactory" ref="sessionFactory"></property>
3 </bean>

HibernateDaoSupport提供了如下方法:

public final HibernateTemplate getHibernate();

public final void setSessionFactory(SessionFactory sessionFactory)
运行效果:

 1 log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPath 2 log4j:WARN Please initialize the log4j system properly.
3 Hibernate:
4 select
5 max(id)
6 from
7 person
8 Hibernate:
9 insert
10 into
11 spring.person
12 (name, age, sex, id)
13 values
14 (?, ?, ?, ?)
15 Hibernate:
16 select
17 person0_.id as id0_0_,
18 person0_.name as name0_0_,
19 person0_.age as age0_0_,
20 person0_.sex as sex0_0_
21 from
22 spring.person person0_
23 where
24 person0_.id=?
25 Hibernate:
26 update
27 spring.person
28 set
29 name=?,
30 age=?,
31 sex=?
32 where
33 id=?
34 Hibernate:
35 select
36 person0_.id as id0_0_,
37 person0_.name as name0_0_,
38 person0_.age as age0_0_,
39 person0_.sex as sex0_0_
40 from
41 spring.person person0_
42 where
43 person0_.id=?
44 Hibernate:
45 select
46 person0_.id as id0_,
47 person0_.name as name0_,
48 person0_.age as age0_,
49 person0_.sex as sex0_
50 from
51 spring.person person0_
52 *******************
53 2 hanyuan 21 男
54 3 hongten 21 男
55 4 hongten 21 男
56 5 hello 12 M
57 6 hongten 12 M
58 7 hongten2 12 M
59 8 hw 20 M
60 9 hongten 21 男
61 10 hongten 21 男
62 11 hongten 21 男
63 Hibernate:
64 select
65 person0_.id as id0_0_,
66 person0_.name as name0_0_,
67 person0_.age as age0_0_,
68 person0_.sex as sex0_0_
69 from
70 spring.person person0_
71 where
72 person0_.id=?

结果是一样一样的哦。


spring开发_Spring+Hibernate_HibernateDaoSupport

TAG:java spring hibernate hibernateDaoSupport