spring开发_Spring+Hibernate_HibernateTemplate

原有项目:

http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html

修改的类:

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

HibernateTemplate提供持久层访问模板化,他只需要提供一个SessionFactory的引用,就可以执行持久化操作。

SessionFactory对象可以通过Annotation的@Resource获取,也可以通过设值(setter)方式传入。

我们借助于HIbernateTemplate实现持久层的简洁性,大部分CRUD操作只需要一行代码就可以做到,

完全可以避免Hibernate持久化操作那些繁琐的步骤了。

 1 package com.b510.service.impl;
2
3 import java.util.List;
4
5 import javax.annotation.Resource;
6
7 import org.hibernate.SessionFactory;
8 import org.springframework.orm.hibernate3.HibernateTemplate;
9 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional;
11
12 import com.b510.domain.Person;
13 import com.b510.service.PersonService;
14
15 /**
16 * 使用注解方式进行事务管理
17 *
18 * @author Hongten
19 *
20 */
21 @Transactional
22 public class PersonServiceBean implements PersonService {
23 /**
24 * 通过bean.xml配置文件按名称sessionFactory注入属性sessionFactory,
25 * 当sessionFactory注入成功后,我们可以得到Session对象
26 */
27 @Resource
28 private SessionFactory sessionFactory;
29 /**
30 * 定义HibernateTemplate对象,用于执行持久化操作
31 */
32 private HibernateTemplate hibernateTemplate;
33
34 private HibernateTemplate getHibernateTemplate() {
35 if (hibernateTemplate == null) {
36 hibernateTemplate = new HibernateTemplate(sessionFactory);
37 }
38 return hibernateTemplate;
39 }
40
41 @Override
42 public void delete(Integer id) {
43 getHibernateTemplate().delete(getPerson(id));
44 }
45
46 // 在查询的时候,不需要开启事务,并且指定为只读,这样可以提高查询效率
47 @Override
48 @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
49 public Person getPerson(Integer id) {
50 return (Person) getHibernateTemplate().get(Person.class, id);
51 }
52
53 // 在查询的时候,不需要开启事务,并且指定为只读,这样可以提高查询效率
54 @Override
55 @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
56 @SuppressWarnings("unchecked")
57 public List<Person> getPerson() {
58 return getHibernateTemplate().find("from Person");
59 }
60
61 @Override
62 public void save(Person person) {
63 getHibernateTemplate().save(person);
64 }
65
66 @Override
67 public void update(Person person) {
68 getHibernateTemplate().update(person);
69 }
70 }

运行效果:

 1 log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
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 Hibernate:
63 select
64 person0_.id as id0_0_,
65 person0_.name as name0_0_,
66 person0_.age as age0_0_,
67 person0_.sex as sex0_0_
68 from
69 spring.person person0_
70 where
71 person0_.id=?

运行效果是一样一样的。

posted @ 2012-03-15 20:48  Hongten  阅读(6585)  评论(2编辑  收藏  举报
Fork me on GitHub