这篇文章是在SpringMVC的基础上对数据持久层JPA的整合,实现了应用层和数据库的数据交互。在整合JPA前,请先参照下面第一篇博文搭建好SpringMVC框架。
一. 和本篇博文相关的一些基础知识请参考下面几篇博文:
SpringMVC框架的搭建和配置详解请参考:http://blog.csdn.net/jianyuerensheng/article/details/51258942。
JPA的ORM框架原理介绍请参考:http://blog.csdn.net/jianyuerensheng/article/details/50804360。
JPA生命周期、映射关系详解请参考:http://blog.csdn.net/jianyuerensheng/article/details/50819155。
二.JAP的配置过程:
项目整体结构如下:
1.在web.xml文件中进行配置
<!-- 引入 applicationContext.xml配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<!-- 解决JPA因EntityManager关闭导致的延迟加载例外(异常) -->
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2. 添加Spring上下文配置文件applicationContext.xml.
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
htt

本文档详细介绍了如何在已有的SpringMVC框架基础上整合JPA,实现数据持久层与应用层的交互。首先,文章建议读者熟悉SpringMVC的搭建和JPA的基本概念。接着,它阐述了整合JPA的步骤,包括web.xml的配置、Spring上下文配置文件applicationContext.xml的添加、JPA配置文件persistence.xml的设置、数据库配置以及Repository和Controller的编写。通过实例展示了整合后的运行效果。
最低0.47元/天 解锁文章

125

被折叠的 条评论
为什么被折叠?



