26-SpringBoot——核心-Spring Data REST
来源:互联网 发布:pe工具 知乎 编辑:程序博客网 时间:2024/06/08 14:17
Spring Boot核心-Spring Data REST
【博文目录>>>】
【项目源码>>>】
什么是Spring Data REST
Spring Data JPA 是基于Spring Data 的repository 之上,可以将repository 自动输出为REST资源。目前Spring Data REST 支持将Spring Data JPA 、Spring Data MongoDB 、Spring Data Neo4j 、Spring Data GemFire 以及Spring Data Cassandra 的repository 自动转换成REST 服务。
SpringMVC 中配置使用Spring Data REST,Spring Data REST 的配置是定义在RepositoryRestMvcConfiguration ( org.springframework.data.rest. webmvc. Config.RepositoryRestMvcConfiguration )配置类中已经配置好了,我们可以通过继承此类或者直接在自己的配置类上@Import 此配置类。
继承方式
导入方式
Spring Boot 的支持
Spring Boot 对Spring Data REST 的自动配置放置在Rest 中,通过SpringBootRepositoryRestMvcConfiguration 类的源码我们可以得出, Spring Boot 已经为我们自动配置了RepositoryRestConfiguration ,所以在Spring Boot 中使用Spring Data REST只需引入spring-boot-starter-data-rest 的依赖,无须任何配置即可使用。Spring Boot 通过在application.properties 中配置以“ spring.data.rest ”为前缀的属性来配置Repository RestConfiguration。
【代码实现】
application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc\:mysql\://localhost\:3306/springbootspring.datasource.username=rootspring.datasource.password=123456#1spring.jpa.hibernate.ddl-auto=update#2spring.jpa.show-sql=truespring.data.rest.base-path= /apispring.jackson.serialization.indent_output=true
data.sql
INSERT INTO person (id, name, age, address) VALUES (1, '王俊超', 32, '合肥');INSERT INTO person (id, name, age, address) VALUES (2, 'xx', 31, '北京');INSERT INTO person (id, name, age, address) VALUES (3, 'yy', 30, '上海');INSERT INTO person (id, name, age, address) VALUES (4, 'zz', 29, '南京');INSERT INTO person (id, name, age, address) VALUES (5, 'aa', 28, '武汉');INSERT INTO person (id, name, age, address) VALUES (6, 'bb', 27, '合肥');
package org.springframework.boot.rest.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * Author: 王俊超 * Date: 2017-07-18 07:38 * All Rights Reserved !!! */@Entitypublic class Person { @Id @GeneratedValue private Long id; private String name; private Integer age; private String address; public Person() { super(); } public Person(Long id, String name, Integer age, String address) { super(); this.id = id; this.name = name; this.age = age; this.address = address; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
package org.springframework.boot.rest.dao;import org.springframework.boot.rest.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.query.Param;import org.springframework.data.rest.core.annotation.RepositoryRestResource;import org.springframework.data.rest.core.annotation.RestResource;/** * Author: 王俊超 * Date: 2017-07-18 07:37 * All Rights Reserved !!! */@RepositoryRestResource(path = "people")public interface PersonRepository extends JpaRepository<Person, Long> { @RestResource(path = "nameStartsWith", rel = "nameStartsWith") Person findByNameStartsWith(@Param("name") String name);}
package org.springframework.boot.rest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Author: 王俊超 * Date: 2017-07-18 07:43 * All Rights Reserved !!! */@SpringBootApplicationpublic class SampleApplication { public static void main(String[] args) { SpringApplication.run(SampleApplication.class, args); }}
- 26-SpringBoot——核心-Spring Data REST
- SpringBoot之Spring Data REST
- 25-SpringBoot——核心-Spring Data JPA
- springboot data rest 2
- springboot data rest 4
- Spring-Data-Rest 构建Spring-boot Web应用—— rest风格访问JPA数据
- 30-SpringBoot——核心-企业级开发-Spring Security
- 8.3 Spring Data REST
- Spring Data REST
- Spring-data-rest 和Spring-data-jpa
- spring boot data rest 3
- springboot整合spring data jpa
- springboot集成spring-data-jpa
- spring boot 使用 spring data rest
- 18-SpringBoot——核心-基本配置
- 19-SpringBoot——核心-Profile配置
- 20-SpringBoot——核心-运行原理
- 21-SpringBoot——核心-Web配置
- hdu 1023
- 001
- 【tyvj1068】STR(KMP||hash)
- 关于MySQL升级和安装中Table 'performance_schema.session_variables' doesn't exist(某表不存在)的错误
- win10下安装web3失败问题
- 26-SpringBoot——核心-Spring Data REST
- SOCKET用户接口与系统调用关系(2)
- YP.2.2 Integer Data Types and 2.3 2's Complement Integers(双语)
- RxJava源码解析03-变换过程(map)
- Idea远程debug部署在Linux上的tomcat
- Oracle学习笔记day07——视图
- YP.2.4 Binary-Decimal Conversion(双语)
- jq拼接字符串渲染
- Android 进度条源码