上篇博客中已经讲解Spring配置数据源的常用的几种方式。接下来本篇博客继续讲解。配置完数据源,那Dao层是如何访问数据库呢?
基于最近的项目使用SSH2框架完成,分析总结Spring与Hibernate集成后,Dao层访问数据库的常用的两种方式。
至于为什么持久层用Hibernate框架?请参考我以前博客《Hibernate总结一》《Hibernate总结二》《Hibernate总结三》
至于为什么要用Spring框架?请参考我以前博客《spring——控制反转》
至于为什么要用Spring管理Hibernate?请参考我以前博客为什么用Spring来管理Hibernate?
本次例子使用Spring2.0,Hibernate3.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中的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方式。
其中是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来管理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"/>
<!-- 授权管理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
分享到:
相关推荐
用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的Java对象,然后编写基本的DAO(Data Access Objects)接口,并给出Hibernate的DAO实现,采用Hibernate架构实现的DAO类来实现Java类与数据库之间的...
jar包ssi,ssh需要的jar包,hibernate3.jar、ibatis-dao-2.jar、spring.jar、struts.jar、log4j-1.2.9.jar
jar包包括从Struts到spring,再从spring到hibernate的关联,可直接下载放入lib包中
MyEclipse8.0中自带的struts2版本是2.1.6,spring版本有2.0,2.5的,hibernate版本较多些至3.2,首先选版本就选择最优的,struts2没的选只有2.1.6版的,所以先导入struts2支持,然后是spring选的是2.0,问题就出在...
三大框架的jar包,能够跑得起来项目. /xscjManager/WebContent/WEB-INF/lib/antlr.jar /xscjManager/WebContent/WEB-INF/lib/asm.jar /xscjManager/WebContent/WEB-INF/lib/asm-attrs.jar /xscjManager/WebContent/...
ssh框架搭建step by step (springMVC + spring 5.0.4 + hibernate 5.0.12) 好久不弄web了, 周末心血来潮, 使用较新spirng/hibernate搭建一个ssh框架, 供有需要的同学参考/学习/使用. 使用eclipse开发, 搭建,分三步: ...
hibernate:使用hibernate的jar包 jstl:java的标准标签库 junit:测试用到的jar包 spring:使用spring的jar包 struts2:使用struts2的jar包 * 项目体系分层: cn.itcast.elec.containner:自定义的spring容器,...
其中有整合好的全面的SSH jar包,BBS前台,后台源码,这个小系统的报告,数据库关系分析等。 以下内容摘自报告目录部分(希望大家极力推荐哦): 第三章 SSH框架搭建 第四章 设计思路 4.1 需求分析 4.2 对象确定 4.3...
思路,用spring 管理 struts 的action ,与hibernate 的dao 持久层。 先载入jar包,在myeclipse里 增加 struts 增加 hibernate 增加 spring。 然后去网上下载 最新的ssh jar包,覆盖调 web-info\lib\的所有jar包。 ...
其中有整合好的全面的SSH jar包,BBS前台,后台源码,这个小系统的报告,数据库关系分析等。 以下内容摘自报告目录部分(希望大家极力推荐哦): 第三章 SSH框架搭建 第四章 设计思路 4.1 需求分析 4.2 对象确定 4.3...
对于刚进入职场的朋友适合的一款整合,本人亲自搭建,jar包和配置文件都在里面,划分了dao永久层,service逻辑层,controller控制层,下载直接导入,然后修改部分配置文件就可以使用,亲测有效。如果导入错误太多,...
个人认为由Sun官方支持的EJB规范会越来越流行,此时如果使用基于Annotation的SSH框架很容易转移到Struts+EJB+Spring的项目中,而且使用Annotation,很容易实现0配置,像在这个实例中就一个配置,这样避免了配置文件...
声明式事务管理,include方式的模板管理,视图显示使用EL + struts标签库完成, 业务层 和 DAO 层层次清楚, 所有jar包已经提供,工程完整,可直接使用 完整的eclipse3.2.2 +Myeclipse 5.5 +tomcat6.0测试 JDK6.0...
spring-dao.jar spring-hibernate3.jar spring-ibatis.jar spring-jdbc.jar spring-jdo.jar spring-jpa.jar spring-portlet.jar spring-struts.jar spring-tomcat-weaver.jar spring-toplink.jar spring-web.jar ...
本demo采用struts2+spring4.0+hibernate4.2框架集成(内部包含所有jar包),有最dao最底层的代码,可在项目中直接使用,并实现了一个简单的登陆和查询,代码均测试通过,为打ssh框架的同学提供方便哦
1. 导入jar包 2. 搭建struts2环境 3. 搭建Hibernate环境 4. 搭建Spring环境 5. struts2和spirng整合 6. spring和hibernate整合 7. 在dao中使用HibernateTemplate的对象 8. 配置事务
这个项目是基于SSH框架(Spring+hibernate+struts2)框架来实现搭建的,具体搭建说明可以参考我的博客 里面做了详细的说明 当然里面也包含其他功能需要用到的jar包,对配置以及实体配置都有注释说明,非常清晰,相信...
我的一个自创的spring hibernate struct 整合实例 + 我的自创的购物车,在这个项目中采用ssh整合方式。 使用spring dao 及我自己设计的购物车和分页实现,找了很久没有找到我这种实现方式,觉得很有新意。需要spring...
教学-传智播客-项目视频经典之作巴巴运动网106集-27实现Web层的分页功能源代码 所需要的jar文件: (一)、Hibernate: 位于 "\hibernate-distribution-3.3.2.GA" 目录下的jar文件: hibernate3.jar 位于 "\...
教学-传智播客-项目视频经典之作巴巴运动网106集-28将Web层分页封装成通用模块源代码 所需要的jar文件: (一)、Hibernate: 位于 "\hibernate-distribution-3.3.2.GA" 目录下的jar文件: hibernate3.jar 位于 "\...