没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Spring整合Hibernate的步骤
2008-11-20 19:21 13276人阅读 评论(5) 收藏 举报
hibernatespringclassbeansessionobject为什么要整合Hibernate?
1、使用Spring的IOC功能管理SessionFactory对象
LocalSessionFactoryBean
2、使用Spring管理Session对象
HibernateTemplate
3、使用Spring的功能实现声明式的事务管理
整合Hibernate的步骤:
1、配置SessionFactory(可以自动完成)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
2、配置HibernateTemplate,用于完成数据操作
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
3、让Common继承HibernateDaoSupport类,该类提供了HibernateTemplate的getter和setter方法。
4、将hibernateTemplete注入到Common中
<bean id="common" class="com.aptech.common.Common">
<property name="hibernateTemplate" ref="hibernateTemplete"></property>
</bean>
5、将Common的方法修改成hibernateTemplete的操作。
2008-11-20 19:21 13276人阅读 评论(5) 收藏 举报
hibernatespringclassbeansessionobject为什么要整合Hibernate?
1、使用Spring的IOC功能管理SessionFactory对象
LocalSessionFactoryBean
2、使用Spring管理Session对象
HibernateTemplate
3、使用Spring的功能实现声明式的事务管理
整合Hibernate的步骤:
1、配置SessionFactory(可以自动完成)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
2、配置HibernateTemplate,用于完成数据操作
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
3、让Common继承HibernateDaoSupport类,该类提供了HibernateTemplate的getter和setter方法。
4、将hibernateTemplete注入到Common中
<bean id="common" class="com.aptech.common.Common">
<property name="hibernateTemplate" ref="hibernateTemplete"></property>
</bean>
5、将Common的方法修改成hibernateTemplete的操作。
package com.aptech.common;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.aptech.exception.CommonException;
import com.aptech.htm.HibernateSessionFactory;
/**
* 通用类,不再负责事务处理
* 目标对象
* @author 李赞红
*
* @param <POJO>
*/
public class Common<POJO> extends HibernateDaoSupport implements ICommon<POJO> {
public void insertObject(POJO pojo) throws CommonException {
try {
this.getHibernateTemplate().save(pojo);
} catch (DataAccessException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.aptech.exception.CommonException;
import com.aptech.htm.HibernateSessionFactory;
/**
* 通用类,不再负责事务处理
* 目标对象
* @author 李赞红
*
* @param <POJO>
*/
public class Common<POJO> extends HibernateDaoSupport implements ICommon<POJO> {
public void insertObject(POJO pojo) throws CommonException {
try {
this.getHibernateTemplate().save(pojo);
} catch (DataAccessException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
剩余5页未读,继续阅读
资源评论
liuxu19900226
- 粉丝: 1
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Matlab实现扩展卡尔曼滤波EKF源码+项目说明+超详细注释.zip
- 学生课程实验的流程.doc
- 微信消息防撤回证据记录软件-大飞哥软件自习室.exe
- 2023-04-06-项目笔记 - 第一百二十阶段 - 4.4.2.118全局变量的作用域-118 -2024.05.01
- 课程设计基于python+mediapipe+opencv手势识别系统源码(含示例图片).zip
- 联想L-IG41M v1.0 BIOS
- 学生课程实验的流程.doc
- 基于MATLAB和Simulink通过正运动学和逆运动学设计了PID控制器.zip
- 文档内置图片提取工具 2.0能够将WORD PDF PPT等文档里面的图片解析并提取出来.exe
- 2023-04-06-项目笔记 - 第一百二十阶段 - 4.4.2.118全局变量的作用域-118 -2024.05.01
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功