Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子文章分类:Java编程本文代码是传智播客黎活明老师所讲的《Spring2.5视频教程》各种示例的综合,在此把所有框架升级到最新版本整合一下。所用jar包:Struts:struts-2.1.8.1\lib下除了各种plugin(保留struts2-spring-plugin-2.1.8.1.jar)的所有文件Spring:spring-framework-3.0.0.RELEASE\dist下的所有文件Hibernate:hibernate-distribution-3.3.2.GA下的hibernate3.jar\lib\bytecode\cglib下的cglib-2.2.jar\lib\optional\ehcache下的ehcache-1.2.3.jar\lib\required下的所有文件hibernate-annotations-3.4.0.GA下的hibernate-annotations.jar\lib下的ejb3-persistence.jar,hibernate-commons-annotations.jarhibernate-entitymanager-3.4.0.GA下的hibernate-entitymanager.jar\lib\test下的log4j.jar,slf4j-log4j12.jarMySQL:mysql-connector-java-5.1.10-bin.jarJUnit:4JDK:jdk-6u14-windows-i586.exeTomcat:apache-tomcat-6.0.18.zip项目名:testPerson.javaJava代码packagecom.test.bean;importjava.io.Serializable;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;@EntitypublicclassPersonimplementsSerializable{privateIntegerid;privateStringname;publicPerson(){}publicPerson(Stringname){this.name=name;}@Id@GeneratedValuepublicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}@Column(length=10,nullable=false)publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((id==null)?0:id.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!=obj.getClass())returnfalse;finalPersonother=(Person)obj;if(id==null){if(other.id!=null)returnfalse;}elseif(!id.equals(other.id))returnfalse;returntrue;}}packagecom.test.bean;importjava.io.Serializable;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;@EntitypublicclassPersonimplementsSerializable{privateIntegerid;privateStringname;publicPerson(){}publicPerson(Stringname){this.name=name;}@Id@GeneratedValuepublicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}@Column(length=10,nullable=false)publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((id==null)?0:id.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!=obj.getClass())returnfalse;finalPersonother=(Person)obj;if(id==null){if(other.id!=null)returnfalse;}elseif(!id.equals(other.id))returnfalse;returntrue;}}PersonService.javaJava代码packagecom.test.service;importjava.util.List;importcom.test.bean.Person;publicinterfacePersonService{publicvoidsave(Personperson);publicvoidupdate(Personperson);publicvoiddelete(Integerpersonid);publicPersongetPerson(Integerpersonid);@SuppressWarnings(unchecked)publicListPersongetPersons();}packagecom.test.service;importjava.util.List;importcom.test.bean.Person;publicinterfacePersonService{publicvoidsave(Personperson);publicvoidupdate(Personperson);publicvoiddelete(Integerpersonid);publicPersongetPerson(Integerpersonid);@SuppressWarnings(unchecked)publicListPersongetPersons();}PersonServiceBean.javaJava代码packagecom.test.service.impl;importjava.util.List;importjavax.persistence.EntityManager;importjavax.persistence.PersistenceContext;importorg.hibernate.exception.SQLGrammarException;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;importcom.test.bean.Person;importcom.test.service.PersonService;@TransactionalpublicclassPersonServiceBeanimplementsPersonService{@PersistenceContextEntityManagerem;publicvoiddelete(Integerpersonid){em.remove(em.getReference(Person.class,personid));}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)publicPersongetPerson(Integerpersonid){returnem.find(Person.class,personid);}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)@SuppressWarnings(unchecked)publicListPersongetPersons()throwsSQLGrammarException{returnem.createQuery(selectofromPersono).getResultList();}publicvoidsave(Personperson){em.persist(person);}publicvoidupdate(Personperson){em.merge(person);}}packagecom.test.service.impl;importjava.util.List;importjavax.persistence.EntityManager;importjavax.persistence.PersistenceContext;importorg.hibernate.exception.SQLGrammarException;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;importcom.test.bean.Person;importcom.test.service.PersonService;@TransactionalpublicclassPersonServiceBeanimplementsPersonService{@PersistenceContextEntityManagerem;publicvoiddelete(Integerpersonid){em.remove(em.getReference(Person.class,personid));}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)publicPersongetPerson(Integerpersonid){returnem.find(Person.class,personid);}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)@Suppres