常用的是SessionFactory方式
现在我们使用上篇博客中第四种方式Hibernate数据源方式。Spring配置方式如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:com/config/hibernate.cfg.xml</value>
- </property>
- <property name="mappingLocations">
-
- <list>
- <value>classpath:com/hibernate/*.hbm.xml</value>
- </list>
- </property>
- </beans>
Spring管理Dao层的xml配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <import resource="springConfig.xml"/>
-
- <bean id="userDao" class="com.dao.UserDao">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
-
- </beans>
因为为了管理清晰,所以把spring的核心配置和spring管理Dao层放在两个xml文件中。
若Spring中的Dao的Bean文件这么配置,那么我们看一下Dao层的类写法:
- package com.dao;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.entity.User;
- public class UserDao extends HibernateDaoSupport {
- public void insert(User user){
- this.getHibernateTemplate().save(user);
- }
- }
一种是:既然配置Dao的属性sessionFactory,但是sessionFactory是提供给HibernateDaoSupport,可以打开这个类,看看里面的内容。
使用this.getHibernateTemplate()获得HibernateTemplate()模版类,其中Hibernate模板类中有save,update,delete方法
另一种是Hibernate模板类HibernateTemplate方式。注意:这一种在Hibernate4是用不了,
其中是SessionFactory的基础上,再次封装了Spring提供的HibernateTemplate类。
Spring配置文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:com/config/hibernate.cfg.xml</value>
- </property>
- <property name="mappingLocations">
-
- <list>
- <value>classpath:com/hibernate/*.hbm.xml</value>
- </list>
- </property>
-
- <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
Spring管理Dao层的配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
-
- <import resource="springConfig.xml"/>
-
- <bean id="userDao" class="com.dao.UserDao">
- <property name="hibernateTemplate" ref="hibernateTemplate"></property>
- </bean>
- </beans>
若是使用HibernateTemplate则Dao层类的写法:
- package com.dao;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import com.entity.User;
- public class UserDao2 {
- private HibernateTemplate hibernateTemplate;
- public HibernateTemplate getHibernateTemplate() {
- return hibernateTemplate;
- }
- public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
- this.hibernateTemplate = hibernateTemplate;
- }
- public void insert(User user){
- hibernateTemplate.save(user);
- }
- }
其中Spring注入分为四种方式:常见的就是get/set方法,构造器方式,注解方式,还有不常用的接口方式,可以参考一下我以往的博客《spring——控制反转》,其中对这几种方式做了个对比。
Spring来管理Struts,Hibernate来说,常用的是注解方式,对于上述使用注解方式。
则Dao层的配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <import resource="springConfig.xml"/>
-
- <bean id="userDao" class="com.dao.UserDao"> </bean>
- </beans>
则Dao层类的写法如下:
- package com.dao;
- import javax.annotation.Resource;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import com.entity.User;
- public class UserDao {
- @Resource
- private HibernateTemplate hibernateTemplate;
- public void insert(User user){
- hibernateTemplate.save(user);
- }
- }
这样这个Dao层类不用继承,直接使用注解方式注入HibernateTemplate即可。
总体来说,还是喜欢并且本次项目中使用注入HibernateTemplate方式。
这样访问Dao层基本完成了,但是若是访问的过程出错,那该咋办额?或者访问多个Dao层的过程中一个Dao出错,是不是此时其他的Dao层同时停止呢?呵呵,这块就涉及到事务管理了。Hibernate本身也提供了事务管理,其中Hibernate中提供的事务是自动提交的,那有没有更智能的自动提交呢?呵呵,下一篇博客,我们会继续讲解Spring管理事务的几种方式。
PS:为了方便大家对每个框架的包明了,现在把包分开存放。
下载地址:http://download.csdn.net/detail/llhhyy1989/4501145