Spring使用HibernateDaoSupport操作数据
生活随笔
收集整理的這篇文章主要介紹了
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操作数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7.5 搭建上FTP服务
- 下一篇: SpringCloud统一配置之使用配置