Spring Integration Hibernate instance (top)

Source: Internet
Author: User

This article is mainly to show the integration of spring and hibernate (the above)--oneself is the first, some content is not very standard, is the project can run through, there are good suggestions welcome to come ~

1. Database table: (Simple to test the table, age is the primary key int type, name is varchar type)

2. Project drawing:

The following code and configuration files are given in the order in which they are

3, Personservice:

 PackageService;Importjava.sql.SQLException;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Component;ImportSpring.hibernate.dao.PersonDao;ImportSpring.hibernate.entity.Person; @Component Public classPersonservice {@AutowiredPrivatePersondao DAO;  PublicPersonservice () {System.out.println ("Create Personservice"); }     PublicPerson Getpersonbyid (intID) {        Try {            returnDao.getperson (ID); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return NULL; }     PublicPersondao Getdao () {returnDAO; }     Public voidSetdao (Persondao dao) { This. DAO =DAO; }        }

4. APP (the class where the main function resides)

 Packagespring.hibernate;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importservice. Personservice;ImportSpring.hibernate.entity.Person;/*** Hello world! **/ Public classApp { Public Static voidMain (string[] args) {System.out.println ("Hello world!" ); ApplicationContext CTX=NewClasspathxmlapplicationcontext ("Spring.xml"); Personservice Service= (Personservice) ctx.getbean ("Personservice"); Person P= Service.getpersonbyid (16); System.out.println (P.getage ()+" "+p.getname ()); System.out.println ("End"); }}

5, Persondao

 PackageSpring.hibernate.dao;Importjava.sql.SQLException;Importorg.hibernate.SessionFactory;Importorg.springframework.orm.hibernate3.HibernateTemplate;ImportSpring.hibernate.dao.PersonDao;ImportSpring.hibernate.entity.Person; Public classpersondao{Privatehibernatetemplate hibernatetemplate;  PublicPersondao () {System.out.println ("Create Persondao"); }         Public voidsetsessionfactory (sessionfactory sessionfactory) { This. hibernatetemplate =Newhibernatetemplate (sessionfactory); }         Public voidSaveperson (person person)throwsSQLException {//TODO auto-generated Method StubHibernatetemplate.save (person); }     Public voidDelperson (person person)throwsSQLException {//TODO auto-generated Method StubHibernatetemplate.delete (person); }     Public voidEditperson (person person)throwsSQLException {//TODO auto-generated Method Stubhibernatetemplate.update (person); }     PublicPerson Getperson (intIdthrowsSQLException {//TODO auto-generated Method StubPerson person = (person) hibernatetemplate.get (person).class, id); returnPerson ; }}

6. Person (entity Class)

 Packagespring.hibernate.entity;Importjavax.persistence.Entity;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "Person") Public classPerson {PrivateString name; @IdPrivate intAge ;  PublicPerson () {System.out.println ("Create Person"); }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }    }

7. hibernate.properties (config file)

driver=com.mysql.jdbc.driverdatabaseurl=jdbc:mysql://localhost:3306/testusername=rootpassword=123456

Spring Integration Hibernate instance (top)

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.