当前位置:编程学习 > JAVA >>

分析总结Spring管理Hibernate中Dao层访问数据库常用方式(附SSH的jar包)

基于最近的项目使用SSH2框架完成,分析总结Spring与Hibernate集成后,Dao层访问数据库的常用的两种方式。

至于为什么持久层用Hibernate框架?请参考我以前博客《Hibernate总结一》《Hibernate总结二》《Hibernate总结三》

至于为什么要用Spring框架?请参考我以前博客《spring——控制反转》

至于为什么要用Spring管理Hibernate?请参考我以前博客为什么用Spring来管理Hibernate?

本次例子使用Spring2.0,Hibernate3.0,后面会给出相应的jar包

 

常用的是SessionFactory方式

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

[html] 
<?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> 

<?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配置如下:

[html] 
<?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"/> 
    <!-- 授权管理Dao --> 
   <bean id="userDao" class="com.dao.UserDao">  
      <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean>  
 
</beans> 

<?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"/>
 <!-- 授权管理Dao -->
   <bean id="userDao" class="com.dao.UserDao">
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

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

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

[java] 
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); 
    } 

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方式。

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

Spring配置文件如下:

[html] 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi=&quo

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,