日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Spring使用HibernateDaoSupport操作数据

發(fā)布時間:2023/10/11 综合教程 88 老码农
生活随笔 收集整理的這篇文章主要介紹了 Spring使用HibernateDaoSupport操作数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

spring提供了一個數(shù)據(jù)訪問層的類:org.springframework.orm.hibernate3.support.HibernateDaoSupport。一般是讓

dao繼承該類,然后在dao中定義個set方法用來初始化HibernateDaoSupport的HibernateTemplate或者是

SessionFactory.

因為 HibernateTemplate和SessionFactory都是fanal類型,因此不能被重寫。此時能夠隨便set一個方法,注入

資源,在set方法里傳入SessionFactory或者HibernateTemplate,然后再通過super或者this調(diào)用HibernateDaoSupport的

相應(yīng)set方法。

HibernateDao接口:

package com.dao;

public interface HibernateDao {

}

能夠在里面封裝一些經(jīng)常用法。

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注入?yún)?shù)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是用來被繼承類,能夠封裝一些經(jīng)常使用的方法。



總結(jié)

以上是生活随笔為你收集整理的Spring使用HibernateDaoSupport操作数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。