Spring使用HibernateDaoSupport操作数据
spring提供了一个数据訪问层的类:org.springframework.orm.hibernate3.support.HibernateDaoSupport。一般是让
dao继承该类,然后在dao中定义个set方法用来初始化HibernateDaoSupport的HibernateTemplate或者是
SessionFactory.
因为 HibernateTemplate和SessionFactory都是fanal类型,因此不能被重写。此时能够随便set一个方法,注入
资源,在set方法里传入SessionFactory或者HibernateTemplate,然后再通过super或者this调用HibernateDaoSupport的
相应set方法。
HibernateDao接口:
package com.dao;
public interface HibernateDao {
}
能够在里面封装一些经常用法。
HibernateDaoImpl:
package com.dao; import javax.annotation.Resource; import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao{
@Resource
public void setOne(SessionFactory sessionFactory){
this.setSessionFactory(sessionFactory);
}
}
有set方法之处。就能够注入。setOne注入參数sessionFactory,初始化HibernateDaoSupport的SessionFactory.
applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.*" />
<context:annotation-config />
<!-- <tx:annotation-driven transaction-manager="txManager"/> -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:c3p0.properties</value>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>com.entity</value> </list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> </bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="fooServiceOperation" expression="execution(public * com.service..*.save(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
</beans>
userDaoImpl:
package com.dao; import javax.annotation.Resource; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository; import com.entity.Student;
@Repository(value="userDao")
public class UserDaoImpl extends HibernateDaoImpl implements UserDao{ public void save(Student student){
this.getHibernateTemplate().save(student); }
}
HibernateDao是用来被继承类,能够封装一些经常使用的方法。
Spring使用HibernateDaoSupport操作数据的更多相关文章
- spring中使用HibernateTemplate或HibernateDaoSupport报类型转换错误
使用spring的HibernateDaoSupport的时候.报错例如以下: java.lang.ClassCastException: java.lang.String cannot be cas ...
- Spring面试题汇总
一.Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么? Spring 框架中核心组件有三个:Core.Context 和 Beans.其中最核心的组件就是Beans, Spri ...
- Spring MVC学习笔记——完整的用户登录
1.搭建环境的第一步是导包,把下面这些包都导入工程中 /media/common/工作/Ubuntu软件/SpringMVC_jar包整理/aop/media/common/工作/Ubuntu软件/S ...
- 翻译-使用Ratpack和Spring Boot打造高性能的JVM微服务应用
这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中 ...
- spring框架面试相关问题
Spring 框架中核心组件有三个:Core.Context 和 Beans.其中最核心的组件就是Beans, Spring提供的最核心的功能就是Bean Factory. Spring 解决了的最核 ...
- 使用Ratpack和Spring Boot打造高性能的JVM微服务应用
使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...
- Strut2 spring hibernate 整合
一.创建web项目工程 wzz 点击finish 2.添加spring Jar包 AOP,Core,Persistence Core ,web jar 点击next 点击Finish 3.配置Da ...
- 使用Ratpack与Spring Boot构建高性能JVM微服务
在微服务天堂中Ratpack和Spring Boot是天造地设的一对.它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率.效率以及轻量级部署.他们在服务程序的开发中带来了各自的好处.R ...
- Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库
概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...
随机推荐
- php模版静态化原理
看了一些开源系统的,简单的总结一下php的模板及静态原理. 先贴代码,再做解释. index.php <?php //如果已存在静态页面,直接读取并显示 if(file_exists('inde ...
- js隐藏与显示回到顶部按钮
window.onscroll = function () { if (document.documentElement.scrollTop + document.body.scrollTop > ...
- Python编程Web框架 :Django 从入门到精通
Django是一个高级别的Python Web框架,它鼓励快速开发和干净实用的设计. 现在我们开始学习它. Django学习之 第一章:Django介绍 Django学习之 第二章:Django快速上 ...
- SQL Server数据库备份的几个建议
1.定期进行数据备份(完备或差异备份)和日志备份. 2.使用压缩备份来减少磁盘空间占用和提高备份效率. 3.定期检查磁盘剩余空间和备份文件增长情况,以确保有足够空间进行下一次备份. 4.使用校验和(C ...
- 【Oracle】删除undo表空间时,表空间被占用:ORA-30042: Cannot offline the undo tablespace
特别注意:此办法只用于实在没有办法的时候,因为需要加入oracle中的隐含参数,慎用!!! 1. 先查一下是什么在占用undo SYS@ENMOEDU>select segment_name,o ...
- JVM上的下一个Java——Scala
Scala是一种针对 JVM 将函数和面向对象技术组合在一起的编程语言.Scala编程语言近来抓住了很多开发者的眼球.它看起来像是一种纯粹的面向对象编程语言,而又无缝地结合了命令式和函数式的编程风格. ...
- 关于dlg和pro的问题
微软链接:http://technet.microsoft.com/zh-cn/subscriptions/bb983387.aspx CDialogEx::CDialogEx 构造 CDialogE ...
- 配置OpenCV的Qt开发环境
QT&openCV系列!链接:http://www.cnblogs.com/emouse/category/449213.html 本文链接:http://blog.csdn.net/qiur ...
- input输入值限制
限制输入框只能输入数字并且保留两位小数 <input type= "text" onkeyup="var p2 = parseFloat(value).toFixe ...
- CentOS LiveCD、LiveDVD、BinDVD、netinstall、minimal版区别在哪里
BinDVD版——就是普通安装版,需安装到计算机硬盘才能用,bin一般都比较大,而且包含大量的常用软件,安装时无需再在线下载(大部分情况). minimal版——这个镜像文件用于安装一个非常基本的 C ...