Sh_spring Integration Hibernate

Source: Internet
Author: User

Sping and Hibernate are introduced separately, and the following is the integration of them into one piece.


First, hibernate content


1. Create the PO class.

Package Cn.tgb.domain;//user entity public class User {private Integer id;private string username;private string password; Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}

2. Writing User.hbm.xml     

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public     "-//hibernate/hibernate mapping DTD 3.0//en"    "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">   

3. Writing Hibernate.cfg.xml  


<?

XML version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">


Second, spring content


1.Dao Implementation class: Use Hibernatetemplate to manipulate the PO class.

Package Cn.tgb.dao.impl;import Java.util.list;import Org.springframework.orm.hibernate3.hibernatetemplate;import Cn.tgb.dao.userdao;import Cn.tgb.domain.user;public class Userdaoimpl implements userdao{// Use Hibernatetemplate to manipulate the Po class private Hibernatetemplate hibernatetemplate;//provides the Set method public void Sethibernatetemplate ( Hibernatetemplate hibernatetemplate) {this.hibernatetemplate = hibernatetemplate;} @Overridepublic void Save (user user) {this.hibernateTemplate.save (user);} @Overridepublic void Update (user user) {this.hibernateTemplate.update (user);} @Overridepublic void Delete (user user) {this.hibernateTemplate.delete (user);} @Overridepublic User FindByID (Integer uid) {return this.hibernateTemplate.get (User.class, UID);} @Overridepublic list<user> Findalluser () {return This.hibernateTemplate.find ("from User");}}

2.Service Implementation class: Direct use of the DAO layer to manage transactions through spring TX


Package Cn.tgb.service.impl;import Java.util.list;import Cn.tgb.dao.userdao;import cn.tgb.domain.user;import Cn.tgb.service.userservice;public class Userserviceimpl implements userservice{//introduced Userdaoprivate UserDao userDao;// Provides the Set method public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} @Overridepublic void AddUser (user user) {this.userDao.save (user);} @Overridepublic void UpdateUser (user user) {this.userDao.update (user);} @Overridepublic void DeleteUser (user user) {this.userDao.delete (user);} @Overridepublic User Finduserbyid (Integer uid) {return this.userDao.findById (UID);} @Overridepublic list<user> Findalluser () {return This.userDao.findAllUser ();}}


3. Configure 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:aop=" http://www.spring Framework.org/schema/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "xsi:schemalocation=" http://www.s Pringframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.sprin Gframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPR INGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframewo Rk.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--#1配置sessionFactory, by Localsessionfactorybean load configuration obtained sessionfactory-<bean id= "sessionfactory" class= "org.springframework . Orm.hibernate3.LocalSessionFactoryBean "> <!--#1.1 Determine the location of the configuration file-<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></p Roperty> </bean> <!--#2 hibernatetemplate--<bean id= "hibernatetemplate" class= "Org.spr Ingframework.orm.hibernate3.HibernateTemplate "> <!--need to use Sessionfactory to get session, manipulate PO objects--<property Name= "Sessionfactory" ref= "sessionfactory" ></property> </bean> <!--#3 Dao---<be An id= "Userdao" class= "Cn.tgb.dao.impl.UserDaoImpl" > <property name= "hibernatetemplate" ref= " Hibernatetemplate "></property> </bean> <!--#4 Service--<bean id=" UserService "cl ass= "Cn.tgb.service.impl.UserServiceImpl" > <property name= "Userdao" ref= "Userdao" ></property> </ bean> <!--#5 Transaction Management-<!--#5.1 transaction Manager--<bean id= "Txmanager" class= "Org.springframewo Rk.orm.hibernate3.HibernateTransactionManagER "> <!--must determine sessionfactory.

To get the session, then get the transaction--<property name= "sessionfactory" ref= "Sessionfactory" ></property> </bean> <!--#5.2 transaction notifications (enhanced) to enhance the specified target method-<tx:advice id= "Txadvice" transaction-manager= "Txmanager" > <tx: attributes> <tx:method name= "add*"/> <tx:method name= "update*"/> <tx:method name= "delete*"/> <tx:method name= "find*" read-only= "true"/> </tx:attributes> </tx:advice> <!--#5.3 Using an AOP build agent, Make an enhancement-<aop:config> <!--determine the Pointcut--<aop:pointcut expression= "Execution (* cn.tgb.service). *.*(..))" Id= "Txpointcut"/> <!--declaration facets--<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txpointcut"/> </ Aop:config> </beans>


At this point, spring and hibernate are even integrated. Write a test sample below.


Package Cn.tgb.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Cn.tgb.domain.user;import Cn.tgb.service.userservice;public class TestApp {public static void main (string[] args) {//1. Create a user Username = new users (); Ser.setusername ("Jiangxiao"); User.setpassword ("123");//2. Get sping configuration file String xmlpathstring= " Applicationcontext.xml "; ApplicationContext applicationcontext= New Classpathxmlapplicationcontext (xmlpathstring);//3. Obtain Userserviceuserservice UserService = (userservice) applicationcontext.getbean ("UserService") through the IOC;//4. Run the Join User Action Userservice.adduser (username);}}

   

Analysis Summary:


This mind map probably describes the configuration and onboarding of the configuration file, where the Blue cloud part is the content of spring's integrated hibernate.

When the program executes. By loading the configuration file, all of the configuration information is loaded into the program. Let's say it's convenient to have an extended change or something.

They will continue to introduce their two integration with struts in the future.


Sh_spring Integration Hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.