【Spring】SpringMVC整合JPA

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

这篇文章是在SpringMVC的基础上对数据持久层JPA的整合,实现了应用层和数据库的数据交互。在整合JPA前,请先参照下面第一篇博文搭建好SpringMVC框架。

一. 和本篇博文相关的一些基础知识请参考下面几篇博文:

  1. SpringMVC框架的搭建和配置详解请参考:http://blog.csdn.net/jianyuerensheng/article/details/51258942

  2. JPA的ORM框架原理介绍请参考:http://blog.csdn.net/jianyuerensheng/article/details/50804360

  3. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值