A simple hibernate with spring integration

Source: Internet
Author: User

The following is an introduction to the integration of hibernate with spring, where the integration of the two frameworks depends on the configuration of the Applicationcontext.xml file, which is very simple, but it should be noted that importing the package must be imported to webroot/web-inf/ Lib directory, and hibernate is typically imported before spring because the Sessionfactory class is used for integration,

First, the initialization of work: Create a new project, such as (SSH), in this project to import support for Hibernate, a new table Sstest table, there are fields Id,username,password,id primary health and automatic increase 1

Second, the new datasheet for hibernate mapping, open the Myhibernate view, right-click your sstest table-> select hibernate revers Engineering options, and then put Sstest.hbm.xml The file is mapped to the Hibernatedao package, while the tool hibernate mapping file ..., Java data Object ..., Java data Access Object DAO ... option, the first few files can produce the following several. java files: Basehibernatedao. , Hibernatesessionfactory, Ibasehibernatedao, Sstest,sstestdao, finally you test your code, test successfully, and proceed to the next step

To import support for spring in a project, not only will the Applicationcontext.xml be generated automatically after the project is imported, but the following will appear in this file:

<bean id= "sessionfactory" 

class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "configlocation" 

value= "Classpath:hibernate.cfg.xml" >
</property>
</ Bean>

That's why you have to import hibernate first.

If you are familiar with the spring IOC, you can go directly to the next step, if you do not know, then you can write a relatively simple example to test whether the success, must be in the framework and together, so be careful, otherwise in the future out of the problem is not good to find

Five, into the MyEclipse hibernate view, in the new package (such as Springdao) in the Sstest table mapping, this mapping should be noted: Open the Hibernate Reverse Engineering dialog box Check button Java Under Data Access Object (DAO) (Hibernate 3 only), the DAO type is not the basic DAO option but the spring DAO option, and when you map successfully, Sstest.java appears. Sstestdao.java, Sstest.hbm.xml Several files, you can write a class for testing, such as:

Springdaotest.java

Package org.test;
Import Org.springframework.context.ApplicationContext;
Import 

Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Springdao.sstest;
Import Springdao.sstestdao;
public class Springdaotest {public
static void Main (String a[]) {
ApplicationContext cont = new Classpathxmlappli Cationcontext

("Applicationcontext.xml");
Sstestdao s = (Sstestdao) cont.getbean ("Sstestdao");
Sstestdao s = (Sstestdao) cont.getbean ("Sstestdaoproxy");
Sstest st = new Sstest ();
St.setusername ("Spring");
St.setpassword ("Spring");
S.save (ST);
}
}

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.