分析总结Spring管理Hibernate中Dao层访问数据库两种方式

13 篇文章 0 订阅
11 篇文章 0 订阅

常用的是SessionFactory方式

现在我们使用上篇博客中第四种方式Hibernate数据源方式。Spring配置方式如下:

[html]  view plain  copy
 print ?
  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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  7.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.   
  10.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  11.      <property name="configLocation">  
  12.             <value>classpath:com/config/hibernate.cfg.xml</value>  
  13.         </property>  
  14.     <property name="mappingLocations">   
  15.     <!-- 所有的实体类映射文件 -->  
  16.             <list>  
  17.                 <value>classpath:com/hibernate/*.hbm.xml</value>  
  18.             </list>  
  19.     </property>  
  20. </beans>  

Spring管理Dao层的xml配置如下:

[html]  view plain  copy
 print ?
  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:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  7.     <import resource="springConfig.xml"/>  
  8.     <!-- 授权管理Dao -->  
  9.    <bean id="userDao" class="com.dao.UserDao">   
  10.       <property name="sessionFactory" ref="sessionFactory"></property>  
  11.     </bean>   
  12.   
  13. </beans>  

因为为了管理清晰,所以把spring的核心配置和spring管理Dao层放在两个xml文件中。

Spring中的DaoBean文件这么配置,那么我们看一下Dao层的类写法:

[java]  view plain  copy
 print ?
  1. package com.dao;  
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  3. import com.entity.User;  
  4. public class UserDao extends HibernateDaoSupport {  
  5.     public void insert(User user){  
  6.         this.getHibernateTemplate().save(user);  
  7.     }  
  8. }  

一种是:既然配置Dao的属性sessionFactory,但是sessionFactory是提供给HibernateDaoSupport,可以打开这个类,看看里面的内容。

使用this.getHibernateTemplate()获得HibernateTemplate()模版类,其中Hibernate模板类中有saveupdatedelete方法

 

另一种是Hibernate模板类HibernateTemplate方式。注意:这一种在Hibernate4是用不了,

其中是SessionFactory的基础上,再次封装了Spring提供的HibernateTemplate类。

Spring配置文件如下:

[html]  view plain  copy
 print ?
  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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  7.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.   
  10.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  11.      <property name="configLocation">  
  12.             <value>classpath:com/config/hibernate.cfg.xml</value>  
  13.         </property>  
  14.     <property name="mappingLocations">   
  15.     <!-- 所有的实体类映射文件 -->  
  16.             <list>  
  17.                 <value>classpath:com/hibernate/*.hbm.xml</value>  
  18.             </list>  
  19.     </property>  
  20.       
  21.     <bean name="hibernateTemplate"  class="org.springframework.orm.hibernate3.HibernateTemplate">  
  22.         <property name="sessionFactory" ref="sessionFactory" />  
  23.     </bean>  
  24. </beans>  

Spring管理Dao层的配置如下: 

[html]  view plain  copy
 print ?
  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:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  7.       
  8.     <import resource="springConfig.xml"/>  
  9.     <!-- 授权管理Dao -->  
  10.    <bean id="userDao" class="com.dao.UserDao">   
  11.          <property name="hibernateTemplate" ref="hibernateTemplate"></property>  
  12.    </bean>   
  13. </beans>  
若是使用HibernateTemplate则Dao层类的写法:
[html]  view plain  copy
 print ?
  1. package com.dao;  
  2. import org.springframework.orm.hibernate3.HibernateTemplate;  
  3. import com.entity.User;  
  4. public class UserDao2 {  
  5.     private HibernateTemplate hibernateTemplate;  
  6.     public HibernateTemplate getHibernateTemplate() {  
  7.         return hibernateTemplate;  
  8.     }  
  9.     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
  10.         this.hibernateTemplate = hibernateTemplate;  
  11.     }  
  12.     public void insert(User user){  
  13.           hibernateTemplate.save(user);  
  14.     }  
  15. }  

其中Spring注入分为四种方式:常见的就是get/set方法,构造器方式,注解方式,还有不常用的接口方式,可以参考一下我以往的博客《spring——控制反转,其中对这几种方式做了个对比。

Spring来管理StrutsHibernate来说,常用的是注解方式,对于上述使用注解方式。

Dao层的配置如下:

[html]  view plain  copy
 print ?
  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:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  7.     <import resource="springConfig.xml"/>  
  8.     <!-- 授权管理Dao -->  
  9.    <bean id="userDao" class="com.dao.UserDao"> </bean>   
  10. </beans>  

Dao层类的写法如下:

[java]  view plain  copy
 print ?
  1. package com.dao;  
  2. import javax.annotation.Resource;  
  3. import org.springframework.orm.hibernate3.HibernateTemplate;  
  4. import com.entity.User;  
  5. public class UserDao {  
  6.     @Resource  
  7.     private HibernateTemplate hibernateTemplate;  
  8.            public void insert(User user){  
  9.         hibernateTemplate.save(user);  
  10.     }  
  11. }  

这样这个Dao层类不用继承,直接使用注解方式注入HibernateTemplate即可。

总体来说,还是喜欢并且本次项目中使用注入HibernateTemplate方式。

这样访问Dao层基本完成了,但是若是访问的过程出错,那该咋办额?或者访问多个Dao层的过程中一个Dao出错,是不是此时其他的Dao层同时停止呢?呵呵,这块就涉及到事务管理了。Hibernate本身也提供了事务管理,其中Hibernate中提供的事务是自动提交的,那有没有更智能的自动提交呢?呵呵,下一篇博客,我们会继续讲解Spring管理事务的几种方式。

 

 PS:为了方便大家对每个框架的包明了,现在把包分开存放。

下载地址:http://download.csdn.net/detail/llhhyy1989/4501145

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值