Spring Data Jpa开发学习笔记(四)

时间:2022-09-08 16:13:01

本次在http://blog.csdn.net/xyc_csdn/article/details/52564149的基础上添加了Controller层并添加了显示页面用于输出列表。

修改pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.xyc</groupId>
<artifactId>chat</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>chat Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<!-- 使用jstl 1.2时注意无需引用standard -->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>chat</finalName>
</build>
</project>

修改applicationContext.xml

<?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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
>


<!-- 开启spring注解 -->
<!-- 不过在使用了component-scan之后,annotation-config就不需要了 -->
<context:annotation-config/>

<!-- 在指定的package下扫描以及注册javabean -->
<context:component-scan base-package="com.xyc.jpa">
<context:exclude-filter type="regex" expression="com.xyc.jpa.controller"/>
</context:component-scan>

<!-- 实体管理工厂 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="chatUnit"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"></property>
<property name="database" value="MYSQL"></property>
</bean>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<!-- 基于注解的方式使用事务配置声明 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>

<!-- 自动扫描并注入Spring Data JPA 需要在 <beans> 标签中增加对 jpa 命名空间的引用 -->
<jpa:repositories base-package="com.xyc.jpa.dao"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>


</beans>

修改spring-servlet.xml.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 设置使用注解的类所在的包 -->
<context:component-scan base-package="com.xyc.jpa.controller"/>

<!-- 启用 spring mvc 注解 -->
<!-- 注意这里使用的并不是<context:annotation-config />,<context:annotation-config />是启用 spring 注解的 -->
<mvc:annotation-driven/>

<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

创建UserController

package com.xyc.jpa.controller;

import com.xyc.jpa.pojo.User;
import com.xyc.jpa.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;

/**
* Created by xyc on 2016/9/17 0017.
*/

@Controller
@RequestMapping("/api/users")
public class UserController {
@Resource
private UserService userService;

@RequestMapping(method = RequestMethod.GET)
public String list(Map<String, Object> model) {
System.out.println("list");
Collection<User> users = this.userService.queryAll();
model.put("users", users);
return "list";
}
}

修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>

<!-- 使用jstl 1.2时注意web-app头部需要如上所示为2.5或2.5版本以上 -->
<display-name>JPA</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<!--
指定当Web应用启动时,装载Servlet的次序。
当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.
当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它
-->

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

创建list.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--注意应该使用如上引用<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>--%>
<%--
Created by IntelliJ IDEA.
User: xyc
Date: 2016/9/17 0017
Time: 18:18
To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>列表</title>
</head>
<body>
<c:forEach items="${users}" var="user">
<div>${user.username}</div>
</c:forEach>
</body>
</html>

启动服务,打开浏览器访问http://localhost:8080/api/users,页面显示用户列表。