(六)配置及用法之JPA
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。本文简要介绍一下JPA的配置和用法。简要说明~如果不足之处,请留言,谢谢!
需要的包是什么,我也不大清楚,反正把SSH2整合在一起的包放进去就对啦~~呵呵~
本文有的xml使用了//注解方式,那样是错误的。在这里只是为了方便观看。。
(1)web.xml的配置:
(2)applicationContext.xml的配置:
(3)配置persistence.xml(在src下建立META-INF,放在其中):
(4)JPA的应用://如果只是想简单的进行测试 EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa"); EntityManager entityManager= emf.createEntityManager(); entityManager.getTransaction().begin(); entityManager.persist(type); entityManager.getTransaction().commit(); //如果要使用Spring管理的测试的话,老老实实这样做把~ ApplicationContext act = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");//applicationContext.xml在WEB-INF目录下 // ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");applicationContext.xml在src目录下UserServiceImpl userServiceImpl = (UserServiceImpl)act.getBean("userServiceImpl")// 括号内是Bean的ID名//之后就是你的测试内容了~~
如果不足之处,请留言,谢谢。
总结:
(1)主要配置过程:web.xml+applicationContext.xml+persistence.xml