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 此配置类。

继承方式


26-SpringBoot——核心-Spring Data REST_第1张图片

导入方式


这里写图片描述

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。

26-SpringBoot——核心-Spring Data REST_第2张图片

【代码实现】


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 {
    @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);
    }
}

你可能感兴趣的:(Spring,Boot,&,Spring,Cloud,SpringBoot教程,spring,springmvc,springboot,数据库)