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

2014-11-24 09:07:00 · 作者: · 浏览: 4

基于最近的项目使用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" >
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">



classpath:com/config/hibernate.cfg.xml




classpath:com/hibernate/*.hbm.xml


< xml version="1.0" encoding="UTF-8" >
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">



classpath:com/config/hibernate.cfg.xml




classpath:com/hibernate/*.hbm.xml



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

[html]
< xml version="1.0" encoding="UTF-8" >
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">






< xml version="1.0" encoding="UTF-8" >
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">




因为为了管理清晰,所以把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.g