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操作数据的更多相关文章

  1. spring中使用HibernateTemplate或HibernateDaoSupport报类型转换错误

    使用spring的HibernateDaoSupport的时候.报错例如以下: java.lang.ClassCastException: java.lang.String cannot be cas ...

  2. Spring面试题汇总

    一.Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么? Spring 框架中核心组件有三个:Core.Context 和 Beans.其中最核心的组件就是Beans, Spri ...

  3. Spring MVC学习笔记——完整的用户登录

    1.搭建环境的第一步是导包,把下面这些包都导入工程中 /media/common/工作/Ubuntu软件/SpringMVC_jar包整理/aop/media/common/工作/Ubuntu软件/S ...

  4. 翻译-使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中 ...

  5. spring框架面试相关问题

    Spring 框架中核心组件有三个:Core.Context 和 Beans.其中最核心的组件就是Beans, Spring提供的最核心的功能就是Bean Factory. Spring 解决了的最核 ...

  6. 使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...

  7. Strut2 spring hibernate 整合

    一.创建web项目工程 wzz 点击finish 2.添加spring Jar包   AOP,Core,Persistence Core ,web jar 点击next 点击Finish 3.配置Da ...

  8. 使用Ratpack与Spring Boot构建高性能JVM微服务

    在微服务天堂中Ratpack和Spring Boot是天造地设的一对.它们都是以开发者为中心的运行于JVM之上的web框架,侧重于生产率.效率以及轻量级部署.他们在服务程序的开发中带来了各自的好处.R ...

  9. Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库

    概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...

随机推荐

  1. vim插件系列

    http://foocoder.com/blog/mei-ri-vimcha-jian-ping-hua-gun-dong-accelerated-smooth-scroll-dot-vim.html ...

  2. NAS与SAN有什么区别?

    NAS和SAN字面上相似,并且都是新型数据存储模式,但这二者是完全不同的,针对不同方向的技术,为了能够更好的区分它们,天伟数据恢复整理了以下内容供读者参考(天伟数据恢复建议重要数据多备份,备份很重要以 ...

  3. python笔记:字符编码

    ASCII编码 知识点:计算机中最小的单位是bit,bit就咱们常说一位二进制,一位二进制要么是0 要么是 1.但是bit这个单位太小了,我们用字节(byte)来表示.换算的规则如下: 8b = 1B ...

  4. 不用任何插件,实现一个tab栏切换

    //使用jquery中获取当前索引的方法.显示隐藏 <script> $(".tab_list li").on('click', function () { $(thi ...

  5. ORA-03137 - ORA-12592 TNS:BAD PACKET OR ORA-3137 故障处理

    环境 操作系统:CentOS release 6.8 数据库:oracle 11.2.0.4.190115 说明:数据库psu 为19年1月份的补丁,可不间断运行,但是开发提示在执行一些批处理的时候, ...

  6. 前端面试基础-html篇之CSS3新特性

    CSS3的新特性(个人总结)如下 过度(transiton) 动画(animation) 形状转换 transform:适用于2D或3D转换的元素 transform-origin:转换元素的位置(围 ...

  7. (转)19 个 JavaScript 有用的简写技术

    1.三元操作符 当想写if...else语句时,使用三元操作符来代替. const x = 20; let answer; if (x > 10) { answer = 'is greater' ...

  8. SpringMVC(二)@RequestMapping

    学习@RequestMapping注解,参考Spring API 1.@RequestMapping可以修饰在类类型和方法上      ①.修饰在类定义上:   提供初步的URL映射,相对于web应用 ...

  9. Python基础:条件判断 &&循环

    1:条件判断 2:循环 2.1:for 2.2  while 小结: continue :跳出本次循环 进行下次循环,  break :结束循环体.

  10. CentOS 7添加开机启动服务/脚本

    一.添加开机自启服务 在CentOS 7中添加开机自启服务非常方便,只需要两条命令(以 jenkins 为例):systemctl enable jenkins.service #设置jenkins服 ...