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

2014-11-24 09:07:00 · 作者: · 浏览: 2
etHibernateTemplate().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" >
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层的配置如下:

[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">






若是使用HibernateTemplate则Dao层类的写法:[html] view plaincopyprint
p