Integration steps for Hibernate and spring integration

Source: Internet
Author: User

Hibernate and Spring Integration steps:

First step: Join Hibernate and spring related jar packages


Step Two: Create the Application.xml file, configure the DataSource and session,

Step three: Inject the Sessionfactory object into the DAO implementation class and manipulate the database

<?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" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd "><bean id=" Userdao "class=" Com.hcj.dao.impl.UserDaoImpl "><!-- Inject sessionfactory into the DAO implementation class--><property Name= "Sessionfactory" ref= "Sessionfactory" ></property></ Bean><bean id= "Userserver" class= "Com.hcj.service.impl.UserServiceImpl" ><!-- Add Userdao to Userserviceimpl object dynamically--><property name= "Userdao" ref= "Userdao" ></property></bean> <!--configuration DataSource--><bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" ><property name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver"/><property name= "url "Value=" Jdbc:oracle:thin: @localhost: 1521/orcl " /><property name= "username" value= "Scott"/><property name= "password" value= "123456"/></bean> <!--configuration Sessionfactoty--><bean id= "sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/ ><property name= "Mappingresources" ><list><value>User.hbm.xml</value></list> </property><property name= "hibernateproperties" ><props><prop key= "dialect" > Org.hibernate.dialect.oracle9dialect</prop><prop key= "Show_sql" >true</prop></props>< /property></bean></beans>

finally: Enclose the code for the DAO implementation class and the Userserverimle class

Userserverimpl class

Package Com.hcj.service.impl;import Com.hcj.dao.userdao;import Com.hcj.model.user;import Com.hcj.service.userservice;public class Userserviceimpl implements UserService {private Userdao Userdao; public Userdao Getuserdao () {return userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} @Overridepublic void Save (user user) {userdao.save (user);} Public Userserviceimpl () {}public Userserviceimpl (Userdao userdao) {This.userdao = Userdao;}}
DAO Implementation Class
Package Com.hcj.dao.impl;import Java.sql.connection;import Java.sql.sqlexception;import java.sql.Statement;import Java.util.collection;import java.util.iterator;import java.util.list;import Java.util.map;import java.util.Set; Import Javax.sql.datasource;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.classic.session;import Com.hcj.dao.userdao;import Com.hcj.model.user;public class UserDaoImpl Implements Userdao {private sessionfactory sessionfactory; public sessionfactory getsessionfactory () {return Sessionfactory;} public void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} @Overridepublic void Save (user user) {Session session = Sessionfactory.opensession (); Transaction tx = Session.begintransaction (); try {session.save (user); Tx.commit ();} catch (Exception e) {if (tx! = null) {TX . rollback ();}} Finally{session.close ();}}}


Integration steps for Hibernate and spring integration

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.