Spring provides a class for the data Access layer: Org.springframework.orm.hibernate3.support.HibernateDaoSupport, which is usually for
DAO inherits the class, and then defines a set method in DAO to initialize the hibernatetemplate of the Hibernatedaosupport or
Sessionfactory.
Because Hibernatetemplate and sessionfactory are both fanal types, they cannot be overridden. At this point, you can set a method randomly, inject
resources, passing in the Set method Sessionfactory or Hibernatetemplate, and then call Hibernatedaosupport through super or this.
Corresponds to the Set method.
Hibernatedao Interface:
Package Com.dao;public interface Hibernatedao {}
Can be used to encapsulate some common methods;
Hibernatedaoimpl:
Package Com.dao;import Javax.annotation.resource;import Org.hibernate.sessionfactory;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import org.springframework.stereotype.Repository ; @Repositorypublic class Hibernatedaoimpl extends Hibernatedaosupport implements hibernatedao{@Resourcepublic void Setone (Sessionfactory sessionfactory) {this.setsessionfactory (sessionfactory);}}
Where there is a set method, it can be injected. Setone injects the parameter sessionfactory, initializes the sessionfactory of the Hibernatedaosupport.
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.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsdhttp://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"/>--><beanclass= " 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 :p ointcut 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 is used to inherit classes that can encapsulate some common methods.
Spring uses Hibernatedaosupport to manipulate data