使用spring集成jpa

上传人:第*** 文档编号:38770447 上传时间:2018-05-07 格式:DOC 页数:10 大小:24.55KB
返回 下载 相关 举报
使用spring集成jpa_第1页
第1页 / 共10页
使用spring集成jpa_第2页
第2页 / 共10页
使用spring集成jpa_第3页
第3页 / 共10页
使用spring集成jpa_第4页
第4页 / 共10页
使用spring集成jpa_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《使用spring集成jpa》由会员分享,可在线阅读,更多相关《使用spring集成jpa(10页珍藏版)》请在金锄头文库上搜索。

1、使使用用Spring集集成成JPA这里 JPA 的实现采用 Hibernate,需要使用到下面的 jar 文件Hiberante 核心包(8 个文件)hibernate-distribution-3.3.1.GAhibernate3.jarlibbytecodecglibhibernate-cglib-repack-2.1_3.jarlibrequired*.jarHiberante 注解包(3 个文件):hibernate-annotations-3.4.0.GAhibernate-annotations.jarlibejb3-persistence.jar、hibernate-common

2、s-annotations.jarHibernate 针对 JPA 的实现包(3 个文件):hibernate-entitymanager-3.4.0.GAhibernate-entitymanager.jarlibtestlog4j.jar、slf4j-log4j12.jar遵循逐步集成的方式,首先集成 JPA 跟 Spring,当这两者集成成功后,我们再集成 Strutspersistence.xml接下来定义实体 beanPerson.javapackagepackage cn.itcast.bean; importimport java.io.Serializable; importi

3、mport javax.persistence.Column; importimport javax.persistence.Entity; importimport javax.persistence.GeneratedValue; importimport javax.persistence.Id; Entity publicpublic classclass Person implementsimplements Serializable / 推荐大家实现,当然不实现也可以 privateprivate Integer id; privateprivate String name; pu

4、blicpublic Person() publicpublic Person(String name) thisthis.name = name; Id GeneratedValue publicpublic Integer getId() returnreturn id; publicpublic voidvoid setId(Integer id) thisthis.id = id; Column(length = 10, nullable = falsefalse) publicpublic String getName() returnreturn name; publicpubli

5、c voidvoid setName(String name) thisthis.name = name; Override publicpublic intint hashCode() finalfinal intint prime = 31; intint result = 1; result = prime * result + (id = nullnull) ? 0 : id.hashCode(); returnreturn result; Override publicpublic booleanboolean equals(Object obj) ifif (thisthis =

6、obj) returnreturn truetrue; ifif (obj = nullnull) returnreturn falsefalse; ifif (getClass() != obj.getClass() returnreturn falsefalse; finalfinal Person other = (Person) obj; ifif (id = nullnull) ifif (other.id != nullnull) returnreturn falsefalse; elseelse ifif (!id.equals(other.id) returnreturn fa

7、lsefalse; returnreturn truetrue; 实体 bean 定义完了,那么我们在 Spring 里面该如何对实体 bean 进行操作呢? 接着编写业务 beanPersonServiceBean.javapackagepackage cn.itcast.service.impl; importimport java.util.List; importimport javax.persistence.EntityManager; importimport javax.persistence.PersistenceContext; importimport org.sprin

8、gframework.transaction.annotation.Propagation; importimport org.springframework.transaction.annotation.Transactional; importimport cn.itcast.bean.Person; importimport cn.itcast.service.PersonService; Transactional /事务是不要我们打开的,我们只需要在这里申明这个 bean 是需要事务管理的 publicpublic classclass PersonServiceBean imple

9、mentsimplements PersonService PersistenceContext /* * 这个注解是由 JPA 规范提供的,Spring 对它也支持,它是用来注入entityManager 这个对象 * Spring 内部是怎样做的呢?当 Spring 解析到PersistenceContext 这个注解的时候,它会从 * 容器的 entityManager 中得到一个 EntityManager 对象,这个过程并不需要我们做,是 Spring 容器帮我们做的 */ privateprivate EntityManager em; /* * 对实体 bean 进行操作的话,就

10、要用到 entityManager 这个对象 */ publicpublic voidvoid save(Person person) em.persist(person); publicpublic voidvoid update(Person person) em.merge(person); publicpublic voidvoid delete(Integer personid) em.remove(em.getReference(Person.classclass, personid); Transactional(propagation = Propagation.NOT_SUP

11、PORTED, readOnly = truetrue) publicpublic Person getPerson(Integer personid) returnreturn em.find(Person.classclass, personid); Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = truetrue) SuppressWarnings(“unchecked“) publicpublic List getPersons() returnreturn em.createQuery(“select

12、 o from person o“).getResultList(); 目前这个类还没有接口,抽取一个接口出来,PersonService.javapackagepackage cn.itcast.service; importimport java.util.List; importimport cn.itcast.bean.Person; publicpublic interfaceinterface PersonService publicpublic voidvoid save(Person person); publicpublic voidvoid update(Person pe

13、rson); publicpublic Person getPerson(Integer personid); publicpublic voidvoid delete(Integer personid); publicpublic List getPersons(); 建好业务 bean 之后,就要对它进行单元测试,我们要养成好的编程习惯,当开发完业务 bean 之后,要对业务方法进行 Junit 单元测试,不要急着跟 web 这一层集成PersonServiceTest.javapackagepackage junit.test; importimport java.util.List;

14、importimport org.junit.BeforeClass; importimport org.junit.Test; importimport org.springframework.context.ApplicationContext; importimport org.springframework.context.support.ClassPathXmlApplicationContext; importimport cn.itcast.bean.Person; importimport cn.itcast.service.PersonService; publicpubli

15、c classclass PersonServiceTest privateprivate staticstatic PersonService personService; BeforeClass /这个方法是在当单元测试 PersonServiceTest 实例被构建出来后就会执行 /可以在这个方法里面做一些初始化的操作 publicpublic staticstatic voidvoid setUpBeforeClass() throwsthrows Exception trytry ApplicationContext applicationContext = newnew Class

16、PathXmlApplicationContext(“beans.xml“); personService = (PersonService)applicationContext.getBean(“personService“); catchcatch (RuntimeException e) e.printStackTrace(); Test publicpublic voidvoid testSave() personService.save(newnew Person(“小张“); Test publicpublic voidvoid testUpdate() Person person = personService.getPerson(1); /. person.setName(“小丽“); personService.updat

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 毕业论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号