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)