日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring2.5整合JPA

發布時間:2024/9/27 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring2.5整合JPA 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在網上找了很多Spring整合JPA的文章,試著去寫了很多但沒有成功,主要原因可能是jar不正確導致的。花了一些時間自已寫了一個小例子,Spring2.5整合JPA(Hibernate實現)。

所需要的Spring2.5的jar包如下:

所需要的JPA的jar包如下:

Spring2.5整合JPA所需要的jar如下:

文件太大javaeye上傳不了,上面的jar下載地址:(http://download.csdn.net/source/1933969)

1,配置我們的Spring配置文件beans.xml內空如下:

Xml代碼
  • <?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:context="http://www.springframework.org/schema/context"?
  • ??? xmlns:aop="http://www.springframework.org/schema/aop"?
  • ??? xmlns:tx="http://www.springframework.org/schema/tx"?
  • ??? xsi:schemaLocation="http://www.springframework.org/schema/beans?
  • ??????????? http://www.springframework.org/schema/beans/spring-beans-2.5.xsd?
  • ??????????? http://www.springframework.org/schema/context????????????
  • ??????????? http://www.springframework.org/schema/context/spring-context-2.5.xsd?
  • ??????????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd?
  • ??????????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">?
  • ?
  • ??? <context:annotation-config? />?
  • ?
  • ??? <bean id="entityManager"?
  • ??????? class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">?
  • ??????? <property name="persistenceUnitName" value="mengya"></property>?
  • ??? </bean>?
  • ?
  • ??? <bean id="JPATranManager"?
  • ??????? class="org.springframework.orm.jpa.JpaTransactionManager">?
  • ??????? <property name="entityManagerFactory" ref="entityManager"></property>?
  • ??? </bean>?
  • ?
  • ??? <tx:annotation-driven transaction-manager="JPATranManager" />?
  • ?
  • ??? <bean id="studentDAO"?
  • ??????? class="com.mengya.dao.imple.StudentDAOImple">?
  • ??? </bean>?
  • ?
  • ??? <bean id="studentSerivce"?
  • ??????? class="com.mengya.service.imple.StudentServiceImple">?
  • ??????? <property name="studao" ref="studentDAO"></property>?
  • ??? </bean>?
  • ?
  • </beans>?
  • <?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><context:annotation-config /><bean id="entityManager"class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"><property name="persistenceUnitName" value="mengya"></property></bean><bean id="JPATranManager"class="org.springframework.orm.jpa.JpaTransactionManager"><property name="entityManagerFactory" ref="entityManager"></property></bean><tx:annotation-driven transaction-manager="JPATranManager" /><bean id="studentDAO"class="com.mengya.dao.imple.StudentDAOImple"></bean><bean id="studentSerivce"class="com.mengya.service.imple.StudentServiceImple"><property name="studao" ref="studentDAO"></property></bean></beans>

    如查以上xml在你的MyEclipse中出顯了一個錯誤提示,請你自手在你的MyEclipse的XML配置中配置http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

    2, 配置JPA的persistence.xml(在src/META-INF/persistence.xml中)內空如下:

    Xml代碼
  • <?xml version="1.0" encoding="UTF-8"?>?
  • <persistence xmlns="http://java.sun.com/xml/ns/persistence"?
  • ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
  • ??? xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"?
  • ??? version="1.0">?
  • ??? <persistence-unit name="mengya" transaction-type="RESOURCE_LOCAL">?
  • ??????? <properties>?
  • ??????????? <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />?
  • ??????????? <property name="hibernate.hbm2ddl.auto" value="update" />?
  • ??????????? <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />?
  • ??????????? <property name="hibernate.connection.username" value="root" />?
  • ??????????? <property name="hibernate.connection.password" value="###" />?
  • ??????????? <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&amp;characterEncoding=gbk" />?
  • ??????? </properties>?
  • ??? </persistence-unit>????
  • </persistence>?
  • <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0"><persistence-unit name="mengya" transaction-type="RESOURCE_LOCAL"><properties><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /><property name="hibernate.hbm2ddl.auto" value="update" /><property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" /><property name="hibernate.connection.username" value="root" /><property name="hibernate.connection.password" value="###" /><property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&amp;characterEncoding=gbk" /></properties></persistence-unit> </persistence>

    3,構建我們的實體Bean如下:

    Java代碼
  • @Entity?
  • public class Student {?
  • ?
  • ??? private Integer stu_id;?
  • ?
  • ??? private String stu_name;?
  • ?
  • ??? private String stu_sex;?
  • ?
  • ??? private Integer stu_age;?
  • ?
  • ??? private String stu_info;?
  • ?
  • ??? @Id?
  • ??? @GeneratedValue(strategy = GenerationType.AUTO)?
  • ??? public Integer getStu_id() {?
  • ??????? return stu_id;?
  • ??? }?
  • ?
  • ??? public void setStu_id(Integer stu_id) {?
  • ??????? this.stu_id = stu_id;?
  • ??? }?
  • ?
  • ??? @Column(nullable = false)?
  • ??? public String getStu_name() {?
  • ??????? return stu_name;?
  • ??? }?
  • ?
  • ??? public void setStu_name(String stu_name) {?
  • ??????? this.stu_name = stu_name;?
  • ??? }?
  • ?
  • ??? public Integer getStu_age() {?
  • ??????? return stu_age;?
  • ??? }?
  • ?
  • ??? public void setStu_age(Integer stu_age) {?
  • ??????? this.stu_age = stu_age;?
  • ??? }?
  • ?
  • ??? public String getStu_info() {?
  • ??????? return stu_info;?
  • ??? }?
  • ?
  • ??? public void setStu_info(String stu_info) {?
  • ??????? this.stu_info = stu_info;?
  • ??? }?
  • ?
  • ??? public String getStu_sex() {?
  • ??????? return stu_sex;?
  • ??? }?
  • ?
  • ??? public void setStu_sex(String stu_sex) {?
  • ??????? this.stu_sex = stu_sex;?
  • ??? }?
  • ?
  • ??? @Override?
  • ??? public int hashCode() {?
  • ??????? final int PRIME = 31;?
  • ??????? int result = 1;?
  • ??????? result = PRIME * result + ((stu_id == null) ? 0 : stu_id.hashCode());?
  • ??????? return result;?
  • ??? }?
  • ?
  • ??? @Override?
  • ??? public boolean equals(Object obj) {?
  • ??????? if (this == obj)?
  • ??????????? return true;?
  • ??????? if (obj == null)?
  • ??????????? return false;?
  • ??????? if (getClass() != obj.getClass())?
  • ??????????? return false;?
  • ??????? final Student other = (Student) obj;?
  • ??????? if (stu_id == null) {?
  • ??????????? if (other.stu_id != null)?
  • ??????????????? return false;?
  • ??????? } else if (!stu_id.equals(other.stu_id))?
  • ??????????? return false;?
  • ??????? return true;?
  • ??? }?
  • ?
  • }?
  • @Entity public class Student {private Integer stu_id;private String stu_name;private String stu_sex;private Integer stu_age;private String stu_info;@Id@GeneratedValue(strategy = GenerationType.AUTO)public Integer getStu_id() {return stu_id;}public void setStu_id(Integer stu_id) {this.stu_id = stu_id;}@Column(nullable = false)public String getStu_name() {return stu_name;}public void setStu_name(String stu_name) {this.stu_name = stu_name;}public Integer getStu_age() {return stu_age;}public void setStu_age(Integer stu_age) {this.stu_age = stu_age;}public String getStu_info() {return stu_info;}public void setStu_info(String stu_info) {this.stu_info = stu_info;}public String getStu_sex() {return stu_sex;}public void setStu_sex(String stu_sex) {this.stu_sex = stu_sex;}@Overridepublic int hashCode() {final int PRIME = 31;int result = 1;result = PRIME * result + ((stu_id == null) ? 0 : stu_id.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;final Student other = (Student) obj;if (stu_id == null) {if (other.stu_id != null)return false;} else if (!stu_id.equals(other.stu_id))return false;return true;}}

    4,構建我們的DAO接口及實現:

    Java代碼
  • public interface StudentDao {?
  • ??? public void save(Student stu);?
  • ?
  • ??? public void delete(Integer stu_id);?
  • ?
  • ??? public void update(Student stu);?
  • ?
  • ??? public Student getStudentByPK(Integer stu_id);?
  • ?
  • ??? public List<Student> queryAll();?
  • }?
  • public interface StudentDao {public void save(Student stu);public void delete(Integer stu_id);public void update(Student stu);public Student getStudentByPK(Integer stu_id);public List<Student> queryAll(); }

    Java代碼
  • public class StudentDAOImple implements StudentDao {?
  • ??? @PersistenceContext?
  • ??? EntityManager em;?
  • ?
  • ??? public void save(Student stu) {?
  • ??????? em.persist(stu);?
  • ??? }?
  • ?
  • ??? public void delete(Integer stu_id) {?
  • ??????? em.remove(em.getReference(Student.class, stu_id));?
  • ??? }?
  • ?
  • ??? public void update(Student stu) {?
  • ??????? em.merge(stu);?
  • ??? }?
  • ?
  • ??? public Student getStudentByPK(Integer stu_id) {?
  • ??????? return em.find(Student.class, stu_id);?
  • ??? }?
  • ?
  • ??? public List<Student> queryAll() {?
  • ??????? return em.createQuery("select s from Student s").getResultList();?
  • ??? }?
  • ?
  • }?
  • public class StudentDAOImple implements StudentDao {@PersistenceContextEntityManager em;public void save(Student stu) {em.persist(stu);}public void delete(Integer stu_id) {em.remove(em.getReference(Student.class, stu_id));}public void update(Student stu) {em.merge(stu);}public Student getStudentByPK(Integer stu_id) {return em.find(Student.class, stu_id);}public List<Student> queryAll() {return em.createQuery("select s from Student s").getResultList();}}

    5,service的接口及實現:

    Java代碼
  • @Transactional?
  • public interface StudentService {?
  • ?
  • ??? public void save(Student stu);?
  • ?
  • ??? public void delete(Integer stu_id);?
  • ?
  • ??? public void update(Student stu);?
  • ?
  • ??? @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)?
  • ??? public Student getStudentByPK(Integer stu_id);?
  • ?
  • ??? @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)?
  • ??? public List<Student> queryAll();?
  • ?
  • }?
  • @Transactional public interface StudentService {public void save(Student stu);public void delete(Integer stu_id);public void update(Student stu);@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)public Student getStudentByPK(Integer stu_id);@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)public List<Student> queryAll();}

    Java代碼
  • public class StudentServiceImple implements StudentService {?
  • ?????
  • ??? private StudentDao studao;?
  • ?????
  • ??? public void delete(Integer stu_id) {?
  • ??????? studao.delete(stu_id);?
  • ??? }?
  • ?
  • ??? public Student getStudentByPK(Integer stu_id) {?
  • ??????? return studao.getStudentByPK(stu_id);?
  • ??? }?
  • ?
  • ??? public List<Student> queryAll() {?
  • ??????? return studao.queryAll();?
  • ??? }?
  • ?
  • ??? public void save(Student stu) {?
  • ??????? studao.save(stu);?
  • ??? }?
  • ?
  • ??? public void update(Student stu) {?
  • ??????? studao.update(stu);?
  • ??? }?
  • ?
  • ??? public void setStudao(StudentDao studao) {?
  • ??????? this.studao = studao;?
  • ??? }?
  • ?
  • }?
  • public class StudentServiceImple implements StudentService {private StudentDao studao;public void delete(Integer stu_id) {studao.delete(stu_id);}public Student getStudentByPK(Integer stu_id) {return studao.getStudentByPK(stu_id);}public List<Student> queryAll() {return studao.queryAll();}public void save(Student stu) {studao.save(stu);}public void update(Student stu) {studao.update(stu);}public void setStudao(StudentDao studao) {this.studao = studao;}}

    事務只需@Transactional及可,Spring2.5自動幫我們提供事務,事務配置在我們service中。

    6,測試我們的service:

    Java代碼
  • public class StudentServiceTest extends TestCase {?
  • ?
  • ??? public void testSave() {?
  • ??????? ApplicationContext context = new ClassPathXmlApplicationContext(?
  • ??????????????? "beans.xml");?
  • ??????? StudentService stuMght = (StudentService) context?
  • ??????????????? .getBean("studentSerivce");?
  • ??????? Student stu = new Student();?
  • ??????? stu.setStu_name("xiaobo");?
  • ??????? stu.setStu_age(22);?
  • ??????? stu.setStu_sex("男");?
  • ??????? stu.setStu_info("C++");?
  • ??????? stuMght.save(stu);?
  • ??????? System.out.println(stu);?
  • ??? }?
  • ?
  • ??? public void testDelete() {?
  • ??????? ApplicationContext context = new ClassPathXmlApplicationContext(?
  • ??????????????? "beans.xml");?
  • ??????? StudentService stuMght = (StudentService) context?
  • ??????????????? .getBean("studentSerivce");?
  • ??????? stuMght.delete(3);?
  • ??? }?
  • ?
  • ??? public void testUpdate() {?
  • ??????? ApplicationContext context = new ClassPathXmlApplicationContext(?
  • ??????????????? "beans.xml");?
  • ??????? StudentService stuMght = (StudentService) context?
  • ??????????????? .getBean("studentSerivce");?
  • ??????? Student stu = stuMght.getStudentByPK(4);?
  • ??????? stu.setStu_age(23);?
  • ??????? stuMght.update(stu);?
  • ??? }?
  • ?
  • ??? public void testGetStudentByPK() {?
  • ??????? ApplicationContext context = new ClassPathXmlApplicationContext(?
  • ??????????????? "beans.xml");?
  • ??????? StudentService stuMght = (StudentService) context?
  • ??????????????? .getBean("studentSerivce");?
  • ??????? Student stu = stuMght.getStudentByPK(5);?
  • ??????? System.out.println(stu);?
  • ??? }?
  • ?
  • ??? public void testQueryAll() {?
  • ??????? ApplicationContext context = new ClassPathXmlApplicationContext(?
  • ??????????????? "beans.xml");?
  • ??????? StudentService stuMght = (StudentService) context?
  • ??????????????? .getBean("studentSerivce");?
  • ??????? List<Student> stuList = stuMght.queryAll();?
  • ??????? for (Student stu : stuList) {?
  • ??????????? System.out.println(stu);?
  • ??????? }?
  • ??? }?
  • ?
  • }?
  • 總結

    以上是生活随笔為你收集整理的Spring2.5整合JPA的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。