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. python 读取二进制文件 转为16进制输出

    示例: #!/usr/bin/env python #encoding: utf-8 import binascii fh = open(r'C:\Temp\img\2012517165556.png ...

  2. Find a way--hdoj

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【JSOI 2008】 球形空间产生器

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1013 [算法] 高斯消元 [代码] #include<bits/stdc++. ...

  4. Node.js:NPM 使用介绍

    ylbtech-Node.js:NPM 使用介绍 1.返回顶部 1. NPM 使用介绍 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: ...

  5. day63-webservice 08.在web项目中配置带有接口的webservice服务

    这个是配置带有接口的WebService的服务. http://localhost:8080/cxf-web-server/service 带有接口的实现类也给它做好了.jaxws:endpoint是 ...

  6. 关于打包压缩几种格式(gzip,bzip2,xz)的试验对比

    要通过脚本进行备份,必然将会应用到压缩技术,这里简单针对几个常见的格式进行测验,从而得到一种合适的方式. 这里以一个应用目录做例子: [root@isj-test-5 mnt]$du -sh * 66 ...

  7. vue-router 原理解析

    “更新视图但不重新请求页面”是前端路由原理的核心之一,

  8. css浮动导致的高度塌陷问题及清楚浮动的方法

    浮动很好用,但是用浮动后,当浮动元素的父级元素没有高度时,就会造成高度塌陷,从而影响布局.下面就从一开接触前端时,渐渐发现解决高度塌陷的问题的方式. 一.给浮动元素的父级元素添加固定的高度css[he ...

  9. DataTable的Select()方法

    DataRow[] partno = dtPack.Select("PK_SOHEAD = " + pk_sohead + " AND PART_NO = '" ...

  10. VHDL之package

    Pacakge Frequently used pieces of VHDL code are usually written in the form of COMPONENTS, FUNCTIONS ...