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

上篇博客中已经讲解Spring配置数据源的常用的几种方式。接下来本篇博客继续讲解。配置完数据源,那Dao层是如何访问数据库呢?

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

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

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

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

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

 

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

</beans>

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

Spring中的DaoBean文件这么配置,那么我们看一下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模板类中有saveupdatedelete方法

 

另一种是Hibernate模板类HibernateTemplate方式。

其中是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"/>
	<!-- 授权管理Dao -->
   <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来管理StrutsHibernate来说,常用的是注解方式,对于上述使用注解方式。

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"/>
	<!-- 授权管理Dao -->
   <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

  • 15
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值