Configure data sources and sessionfactory,sessionfactory to ingest data sources in Applicationcontext.xml. In the DAO layer, the session can be obtained through sessionfactory and persisted.
1 integrating hibernate in the form of annotations:
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/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd "><context:component-scan base-package=" com.* "/><context:annotation-config/ ><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= "annotatedclasses" ><list><value> Com.entity.student</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></beans>
Persistence class:
Package Com.entity;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import javax.persistence.Id; @Entitypublic class Student {@Id @generatedvalueprivate Integer studentid;private String Studentname;public Integer Getstudentid () {return studentid;} public void Setstudentid (Integer studentid) {this.studentid = StudentID;} Public String Getstudentname () {return studentname;} public void Setstudentname (String studentname) {this.studentname = Studentname;}}
DAO Layer:
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 Userdao {private Sessionfactory sessionfactory;public sessionfactory getsessionfactory () {return sessionfactory;} @Resourcepublic void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} public void Save () {try {Session session=sessionfactory.opensession (); Session.begintransaction (); Student s=new Student (); S.setstudentname ("hi"); Session.save (s); Session.gettransaction (). commit (); Session.close () ; System.out.println ("OK");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
2 Integrate Hibernate with configuration:
Persistence class:
Package Com.entity;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import javax.persistence.id;//@Entitypublic class Student {//@Id//@GeneratedValueprivate Integer studentid;private String Studentname;public Integer Getstudentid () {return studentid;} public void Setstudentid (Integer studentid) {this.studentid = StudentID;} Public String Getstudentname () {return studentname;} public void Setstudentname (String studentname) {this.studentname = Studentname;}}
ORM Mapping Student.hbm.xml Configuration:
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
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/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd "><!--<context:component-scan base-package=" com.* "/><context: Annotation-config/>--><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= "annotatedclasses" ><list><value> Com.entity.student</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= "SessionfacTory "class=" Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "><property name=" DataSource "ref=" DataSource "></property><property name=" Mappingresources "><list><value >com/entity/student.hbm.xml</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= "Userdao" class= "Com.dao.UserDao" > <property name= "Sessionfactory" ref= "sessionfactory" ></property></bean><bean id= "UserService "Class=" Com.service.UserService "><property name=" Userdao "ref=" Userdao "></property></bean> </beans>
DAO Layer:
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 Userdao {private Sessionfactory sessionfactory;public sessionfactory getsessionfactory () {return sessionfactory;} @Resourcepublic void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} public void Save () {try {Session session=sessionfactory.opensession (); Session.begintransaction (); Student s=new Student (); S.setstudentname ("hi"); Session.save (s); Session.gettransaction (). commit (); Session.close () ; System.out.println ("OK");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Service Layer:
Package Com.service;import Javax.annotation.resource;import Org.springframework.stereotype.service;import com.dao.userdao;//@Servicepublic class UserService {private Userdao userdao;public Userdao Getuserdao () {return Userdao ;} @Resource (name= "Userdao") public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} public void Save () {Userdao.save ();}}
Test:
Package Com.aop;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.service.userservice;public Class Test {public static void main (string[] args) {ApplicationContext ac=new classpathxmlapplicationcontext (" Applicationcontext.xml "); UserService uc= (UserService) Ac.getbean ("UserService"); Uc.save ();}}
Spring Integration Hibernate