Spring and Hibernate integration
Steps
1) Introduction of JAR Package
Connection pool/Database driver package
Hibernate-related JAR packages
Spring core packages and AOP packages
Spring-orm
Spring-tx
2) configuration
Hibernate.cfg.xml
Bean.xml
3) Build
Package com.cx.entity;/** * Created by Cxspace on 16-8-11. */public class Dept { private int id; private String name; public int getId () { return ID; } public void setId (int id) { this.id = ID; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; }}
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
Package Com.cx.dao;import com.cx.entity.dept;import org.hibernate.sessionfactory;/** * Created by Cxspace on 16-8-11. */public class Deptdao { private sessionfactory sessionfactory; public void Setsessionfactory (Sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } The transaction is given to spring administration public void Save (Dept Dept) { sessionfactory.getcurrentsession (). Save (Dept);} }
Package Com.cx.service;import com.cx.dao.deptdao;import com.cx.entity.dept;/** * Created by Cxspace on 16-8-11. */public class Deptservice { private Deptdao Deptdao; public void Setdeptdao (Deptdao deptdao) { This.deptdao = Deptdao; } public void Save (Dept Dept) { deptdao.save (Dept);} }
<?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:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "Http://www.springframework.org/schema/context" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/beans HT Tp://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP htt P://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPR Ingframework.org/schema/tx/spring-tx.xsd "> <!--DAO Instance--<bean id=" Deptdao "class=" Com.cx.dao.DeptDao "& Gt <property name= "Sessionfactory "ref=" sessionfactory "></property> </bean> <!--service Instance--<bean id=" depts Ervice "class=" Com.cx.service.DeptService "> <property name=" Deptdao "ref=" Deptdao "></property> &L t;/bean> <!--c3p0 connection pool, data source configuration-<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" & Gt <property name= "Driverclass" value= "Com.mysql.jdbc.Driver" ></property> <property name= "Jdbcurl" Valu E= "Jdbc:mysql:///learnhibernate" ></property> <property name= "user" value= "root" ></property> <property name= "Password" value= "33269456.cx" ></property> <property name= "Initialpoolsize" Valu E= "3" ></property> <property name= "maxpoolsize" value= "ten" ></property> <property name = "Maxstatements" value= "></property> <property name=" Acquireincrement "value=" 2 "></property& Gt </bean> <!--mode one: Direct load Hibernate.cfg.xml file mode integration-<!--<bean id= "sessionfactory" class= "org.springframework . Orm.hibernate3.LocalSessionFactoryBean "> <property name=" configlocation "value=" classpath: Hibernate.cfg.xml ></property> </bean> <!--two. Connect pool to Spring management-<!--<bea n id= "sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name= "Co Nfiglocation "value=" Classpath:hibernate.cfg.xml "></property> <property name=" DataSource "ref=" Datasou Rce "></property> </bean>-<!--best Sessionfactory configuration, Spring and hibernate integration Hibernate.cfg.xml Wrote Bean.xml---<bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <!--inject Connection pool Object--<property name=" data Source "ref=" DataSource "></property> <!--Hibernate Common configuration--<property name="Hibernateproperties" > <props> <prop key= "Hibernate.dialect" >org.hibernate.dialec t.mysql5dialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.hbm2ddl.auto" >update</prop> </props> </property> <!--mapping file Path configuration--<property name= "mappinglocations" > <list> <value>classpath:com/ Cx/entity/*.hbm.xml </value> </list> </property> </bean> <! --end of integration--<!--Configure Hibernate transaction manager Class--<bean id= "Txmanager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "></property> </bean> <!--configuration Transaction Enhancements--<tx:advice id=" Txadvice "Transaction-ma Nager= "Txmanager" > <tx:attributes> <tx:metHod name= "*" read-only= "false"/> </tx:attributes> </tx:advice> <!--AOP configuration--<aop:c onfig> <aop:pointcut id= "PT" expression= "Execution (* com.cx.service.*.* (..))" /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "pt"/> </aop:config></beans>
Test
Package Test;import Com.cx.entity.dept;import Com.cx.service.deptservice;import org.junit.test;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Created by Cxspace on 16-8-11. */public class APP { private applicationcontext ac = new Classpathxmlapplicationcontext ("Bean.xml"); @Test public void TestApp () throws exception{ deptservice deptservice = (deptservice) ac.getbean ("Deptservice" ); System.out.println (Deptservice.getclass ()); Deptservice.save (New Dept ());} }
Spring Learning -9-spring and hibernate integration