26-SpringBoot——核心-Spring Data REST

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.Driver
spring.datasource.url=jdbc\:mysql\://localhost\:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
#1
spring.jpa.hibernate.ddl-auto=update
#2
spring.jpa.show-sql=true

spring.data.rest.base-path= /api
spring.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 !!!
 */
@Entity
public 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 !!!
 */
@SpringBootApplication
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值