在网上找了很多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代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  13. <context:annotation-config  />
  14. <bean id="entityManager"
  15. class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
  16. <property name="persistenceUnitName" value="mengya"></property>
  17. </bean>
  18. <bean id="JPATranManager"
  19. class="org.springframework.orm.jpa.JpaTransactionManager">
  20. <property name="entityManagerFactory" ref="entityManager"></property>
  21. </bean>
  22. <tx:annotation-driven transaction-manager="JPATranManager" />
  23. <bean id="studentDAO"
  24. class="com.mengya.dao.imple.StudentDAOImple">
  25. </bean>
  26. <bean id="studentSerivce"
  27. class="com.mengya.service.imple.StudentServiceImple">
  28. <property name="studao" ref="studentDAO"></property>
  29. </bean>
  30. </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代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  5. version="1.0">
  6. <persistence-unit name="mengya" transaction-type="RESOURCE_LOCAL">
  7. <properties>
  8. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
  9. <property name="hibernate.hbm2ddl.auto" value="update" />
  10. <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />
  11. <property name="hibernate.connection.username" value="root" />
  12. <property name="hibernate.connection.password" value="###" />
  13. <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&amp;characterEncoding=gbk" />
  14. </properties>
  15. </persistence-unit>
  16. </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代码
  1. @Entity
  2. public class Student {
  3. private Integer stu_id;
  4. private String stu_name;
  5. private String stu_sex;
  6. private Integer stu_age;
  7. private String stu_info;
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.AUTO)
  10. public Integer getStu_id() {
  11. return stu_id;
  12. }
  13. public void setStu_id(Integer stu_id) {
  14. this.stu_id = stu_id;
  15. }
  16. @Column(nullable = false)
  17. public String getStu_name() {
  18. return stu_name;
  19. }
  20. public void setStu_name(String stu_name) {
  21. this.stu_name = stu_name;
  22. }
  23. public Integer getStu_age() {
  24. return stu_age;
  25. }
  26. public void setStu_age(Integer stu_age) {
  27. this.stu_age = stu_age;
  28. }
  29. public String getStu_info() {
  30. return stu_info;
  31. }
  32. public void setStu_info(String stu_info) {
  33. this.stu_info = stu_info;
  34. }
  35. public String getStu_sex() {
  36. return stu_sex;
  37. }
  38. public void setStu_sex(String stu_sex) {
  39. this.stu_sex = stu_sex;
  40. }
  41. @Override
  42. public int hashCode() {
  43. final int PRIME = 31;
  44. int result = 1;
  45. result = PRIME * result + ((stu_id == null) ? 0 : stu_id.hashCode());
  46. return result;
  47. }
  48. @Override
  49. public boolean equals(Object obj) {
  50. if (this == obj)
  51. return true;
  52. if (obj == null)
  53. return false;
  54. if (getClass() != obj.getClass())
  55. return false;
  56. final Student other = (Student) obj;
  57. if (stu_id == null) {
  58. if (other.stu_id != null)
  59. return false;
  60. } else if (!stu_id.equals(other.stu_id))
  61. return false;
  62. return true;
  63. }
  64. }
@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代码
  1. public interface StudentDao {
  2. public void save(Student stu);
  3. public void delete(Integer stu_id);
  4. public void update(Student stu);
  5. public Student getStudentByPK(Integer stu_id);
  6. public List<Student> queryAll();
  7. }
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代码
  1. public class StudentDAOImple implements StudentDao {
  2. @PersistenceContext
  3. EntityManager em;
  4. public void save(Student stu) {
  5. em.persist(stu);
  6. }
  7. public void delete(Integer stu_id) {
  8. em.remove(em.getReference(Student.class, stu_id));
  9. }
  10. public void update(Student stu) {
  11. em.merge(stu);
  12. }
  13. public Student getStudentByPK(Integer stu_id) {
  14. return em.find(Student.class, stu_id);
  15. }
  16. public List<Student> queryAll() {
  17. return em.createQuery("select s from Student s").getResultList();
  18. }
  19. }
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代码
  1. @Transactional
  2. public interface StudentService {
  3. public void save(Student stu);
  4. public void delete(Integer stu_id);
  5. public void update(Student stu);
  6. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  7. public Student getStudentByPK(Integer stu_id);
  8. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  9. public List<Student> queryAll();
  10. }
@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代码
  1. public class StudentServiceImple implements StudentService {
  2. private StudentDao studao;
  3. public void delete(Integer stu_id) {
  4. studao.delete(stu_id);
  5. }
  6. public Student getStudentByPK(Integer stu_id) {
  7. return studao.getStudentByPK(stu_id);
  8. }
  9. public List<Student> queryAll() {
  10. return studao.queryAll();
  11. }
  12. public void save(Student stu) {
  13. studao.save(stu);
  14. }
  15. public void update(Student stu) {
  16. studao.update(stu);
  17. }
  18. public void setStudao(StudentDao studao) {
  19. this.studao = studao;
  20. }
  21. }
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代码
  1. public class StudentServiceTest extends TestCase {
  2. public void testSave() {
  3. ApplicationContext context = new ClassPathXmlApplicationContext(
  4. "beans.xml");
  5. StudentService stuMght = (StudentService) context
  6. .getBean("studentSerivce");
  7. Student stu = new Student();
  8. stu.setStu_name("xiaobo");
  9. stu.setStu_age(22);
  10. stu.setStu_sex("男");
  11. stu.setStu_info("C++");
  12. stuMght.save(stu);
  13. System.out.println(stu);
  14. }
  15. public void testDelete() {
  16. ApplicationContext context = new ClassPathXmlApplicationContext(
  17. "beans.xml");
  18. StudentService stuMght = (StudentService) context
  19. .getBean("studentSerivce");
  20. stuMght.delete(3);
  21. }
  22. public void testUpdate() {
  23. ApplicationContext context = new ClassPathXmlApplicationContext(
  24. "beans.xml");
  25. StudentService stuMght = (StudentService) context
  26. .getBean("studentSerivce");
  27. Student stu = stuMght.getStudentByPK(4);
  28. stu.setStu_age(23);
  29. stuMght.update(stu);
  30. }
  31. public void testGetStudentByPK() {
  32. ApplicationContext context = new ClassPathXmlApplicationContext(
  33. "beans.xml");
  34. StudentService stuMght = (StudentService) context
  35. .getBean("studentSerivce");
  36. Student stu = stuMght.getStudentByPK(5);
  37. System.out.println(stu);
  38. }
  39. public void testQueryAll() {
  40. ApplicationContext context = new ClassPathXmlApplicationContext(
  41. "beans.xml");
  42. StudentService stuMght = (StudentService) context
  43. .getBean("studentSerivce");
  44. List<Student> stuList = stuMght.queryAll();
  45. for (Student stu : stuList) {
  46. System.out.println(stu);
  47. }
  48. }
  49. }

Spring2.5整合JPA相关推荐

  1. spring boot 系列之四:spring boot 整合JPA

    上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...

  2. 配置spring整合jpa自动生成数据表

    配置spring整合jpa自动生成数据表 applicationContext.xml <?xml version="1.0" encoding="UTF-8&qu ...

  3. (二)SpringBoot 整合 JPA

    一.JPA简介 JPA(java persistence api)并不是一个框架,而是一组规范.Hibernate.TopLink.OpenJPA都实现了JPA规范,不过Hinernate最成功).所 ...

  4. jpa 自定义sql if_SpringBoot整合JPA实现多数据源及读写分离

    SpringBoot整合JPA实现多数据源及读写分离 项目地址:https://github.com/baojingyu/spring-boot-jpa-dynamic-datasource 本项目使 ...

  5. Spring Boot基础学习笔记07:Spring Boot整合JPA

    文章目录 零.学习目标 1.熟悉Spring Data JPA基本语法和使用 2.掌握Spring Boot与JPA的整合使用 一.Spring Data JPA概述 1.Spring Data JP ...

  6. jpa jql 时间范围查询_SpringBoot整合JPA案例

    本节主要学习SpringBoot + JPA(底层使用Hibernate实现)集成案例. 1.JPA概述 1.1 JPA简介 JPA是Java Persistence API的简称,中文名Java持久 ...

  7. Spring Boot整合Jpa多数据源

    Spring Boot整合Jpa多数据源 本文是Spring Boot整合数据持久化方案的最后一篇,主要和大伙来聊聊Spring Boot整合Jpa多数据源问题.在Spring Boot整合JbdcT ...

  8. Springboot整合JPA多数据源(Oracle+Mysql)

    Springboot整合JPA多数据源 1. Maven 2. 基本配置 2.1 DataSource 3. 多数据源配置 3.1 JpaConfigOracle 3.2 JpaConfigMysql ...

  9. JPA 7. Spring 整合 JPA

    Spring 整合 JPA 三种整合方式 LocalEntityManagerFactoryBean:适用于那些仅使用 JPA 进行数据访问的项目,该 FactoryBean 将根据JPA Persi ...

最新文章

  1. 2020 年 Java 面试常见 350 题
  2. Tomcat企业级应用
  3. Hdu 1072 【广搜】.cpp
  4. Java、Android—零碎难记笔试考点(持续更新)
  5. springboot中得注解_Spring以及SpringBoot中的常用的注解小结
  6. 华为做raid5步骤_华为RH2288V5服务器做RAID 0(官方推荐做法)
  7. torch将多个tensor张量合并为一个张量,只提高迷你批次的纬度
  8. Swift - 触摸事件响应机制(UiView事件传递)
  9. App列表之拖拽ListView(上)
  10. wp login.php 打不开,解决wordpress后台无法登录或显示空白的问题
  11. 数据库系统工程师考点笔记
  12. Win11预览版更新错误怎么办?Win11预览版安装失败的解决方法
  13. 内存管理之页转换 virt_to_page
  14. 《21世纪的书:信息时代商业思想10×10阅读》書目信息
  15. 肯德尔秩相关系数matlab,常用的特征选择方法之 Kendall 秩相关系数
  16. win32如何处理组合键盘消息
  17. 知识图谱-KGE-模型:概述【KGE模型充当打分函数的作用】【负采样】【不同模型在不同KG上的表现不一致,需要尝试对比】
  18. Java导出多个excel并压缩下载
  19. 屏蔽 macOS 系统更新提示及清除更新标记
  20. web的前端和后端之分

热门文章

  1. 文本相似度几种计算方法及代码python实现
  2. wxWidgets:wxWindowUpdateLocker类用法
  3. boost::units::quantity相关的测试程序
  4. boost::spirit模块实现将由某个分隔符分隔的任意键/值对解析为 std::map的测试程序
  5. boost::range模块reversed相关的测试程序
  6. boost::contract模块实现move的测试程序
  7. Boost:使用max_element()算法以及transform_iterator和length()函数来查找最长的 向量数组中的4分量向量
  8. Boost:嵌入PTX汇编指令 直接将其添加到boost.compute函数中
  9. Boost:alignment对齐的测试程序
  10. ITK:计算网格的法线