Spring Data JPA

本贴最后更新于 2746 天前,其中的信息可能已经时移世易

pom.xml

version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">

4.0.0modelVersion>

com.ejtgroupId>

JerseyDemoartifactId>

1.0-SNAPSHOTversion>

jarpackaging>

rest-versioningname>

Demo project for spring boot jersey restdescription>

org.springframework.bootgroupId>

spring-boot-starter-parentartifactId>

1.4.2.RELEASEversion>

parent>

<java.version>1.8java.version>

properties>

org.springframework.bootgroupId>

spring-boot-starter-jerseyartifactId>

dependency>

org.springframework.datagroupId>

spring-data-jpaartifactId>

1.10.5.RELEASEversion>

dependency>

org.springframework.datagroupId>

spring-data-rest-webmvcartifactId>

2.5.5.RELEASEversion>

dependency>

org.springframework.bootgroupId>

spring-boot-starter-data-jpaartifactId>

dependency>

mysqlgroupId>

mysql-connector-javaartifactId>

dependency>

dependencies>

org.springframework.bootgroupId>

spring-boot-maven-pluginartifactId>

plugin>

org.apache.maven.pluginsgroupId>

maven-compiler-pluginartifactId>

1.7source>

1.7target>

configuration>

plugin>

plugins>

build>

project>

实体类

package com.ejt.jersey.entity;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

/**

  • Created by xushixin on 2016/12/8.

*/

@Entity

public class User {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private int id;

private String name;

private int age;

  protected User() {

}

public User(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

这里有一个 Customer 类有三个属性,id,firstName 和 lastName。 你也有两个构造函数。 默认构造函数只存在为了 JPA 的目的。 你不会直接使用它,所以它被指定为 protected。 另一个构造函数是用于创建要保存到数据库的 Customer 实例的构造函数。

Customer 类用 @Entity 注释,表示它是一个 JPA 实体。 对于缺少 @Table 注释,假设此实体将映射到名为 Customer 的表。

该 Customer 的 id 属性与注解 @Id 使 JPA 将其识别为对象的 ID。 使用注解 @GeneratedValue 表明,应该自动生成 ID 的策略。

另外两个属性 name 和 age 没有留下注释。 假设它们将被映射到与属性本身是相同名称的列。

Controller 类

package com.ejt.jersey.cotroller;

/**

  • Created by xushixin on 2016/12/9.

*/

import java.util.List;

import com.ejt.jersey.entity.User;

import org.springframework.data.repository.PagingAndSortingRepository;

import org.springframework.data.repository.query.Param;

import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")

public interface PersonRepository extends PagingAndSortingRepository<User, Long> {

List findByName(@Param("name") String name);

}

此存储库是一个接口,将允许您执行涉及 Person 对象的各种操作。 它通过扩展 Spring Data Commons 中定义的 PagingAndSortingRepository 接口来获取这些操作。

在运行时,Spring Data REST 将自动创建此接口的实现。 然后,它将使用 @RepositoryRestResource 注释来指示 Spring MVC 在/ people 处创建 RESTful 端点。

要导出存储库,不需要 @RepositoryRestResource。 它仅用于更改导出详细信息,例如使用/ people 而不是/ users 的默认值。

在这里,您还定义了一个自定义查询,以基于 Name 检索 User 对象的列表。 您将在本指南中了解如何进一步调用。

package com.ejt.jersey;

/**

  • Created by xushixin on 2016/12/8.

*/

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringBootJerseyApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootJerseyApplication.class, args);

}

}

@SpringBootApplication 是一个方便的注释,添加以下所有内容:

  • @Configuration 将类标记为应用程序上下文的 bean 定义的源。

    • @EnableAutoConfiguration 告诉 Spring Boot 根据类路径设置,其他 bean 和各种属性设置开始添加 bean。

      • 通常你会为 Spring MVC 应用程序添加 @EnableWebMvc,但是 Spring Boot 在类路径上看到 spring-webmvc 时会自动添加它。 这将应用程序标记为 Web 应用程序,并激活关键行为,如设置 DispatcherServlet。

      • @ComponentScan 告诉 Spring 在 jersey 包中查找其他组件,配置和服务,使它能够找到控制器。

main()方法使用 Spring Boot 的 SpringApplication.run()方法来启动应用程序。 你有没有注意到没有一行 XML? 也没有 web.xml 文件。 这个 Web 应用程序是 100%纯 Java,你不必处理配置任何管道或配置文件。

Spring Boot 自动启动 Spring Data JPA 以创建 PersonRepository 的具体实现,并将其配置为使用 JPA 与后端数据库通信。

Spring Data REST 构建在 Spring MVC 之上。 它创建了一个 Spring MVC 控制器,JSON 转换器以及提供 RESTful 前端所需的其他 bean 的集合。 这些组件链接到 Spring Data JPA 后端。 使用 Spring Boot 这是所有自动配置; 如果你想研究它是如何工作的,你可以从 Spring Data REST 中的 RepositoryRestMvcConfiguration 开始。

配置文件 application.yml

server:

port: 8080

spring:

datasource:

name: dataSource

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:

username: xushixin

password: ejtone123

initial-size: 1

min-idle: 1

max-wait: 6000

time-between-eviction-runs-millis: 60000

min-evictable-idle-time-millis: 300000

validation-query: select *

test-while-idle: true

test-on-borrow: false

test-on-return: false

Service 层为调用,省略.

Jersey 不能用,路径全部被 JPA 拦截,故 Jersey 代码不再贴出来,请自行查看。

因为路径全部被拦截,还需要某些准备工作,暂时抛开这个问题。注:JerseyConfig.java 里面的 @ApplicationPath 注解是指定 jersey 的路径,如果不设置即为默认根目录,将导致所有路径均不可用,状态码 204(访问成果但是没有任何数据,不做任何操作,也不导向新的地址)。

  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1083 引用 • 3461 回帖 • 262 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...