1. 程式人生 > >Spring Data Rest實戰

Spring Data Rest實戰

一 新建Spring Boot專案

1 新增依賴spring-boot-starter-data-jpa、spring-boot-starter-data-rest和ojdbc6

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.2.0</version>
    </dependency>
</dependencies>

2 配置application.properties

spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe
spring.datasource.username=system
spring.datasource.password=oracle

#配置jpa
#Hibernate提供了根據實體類自動維護資料庫表結構的功能
#create:啟動時刪除上一次生成的表,並根據實體類生成表,表中的資料會被清空
#create-drop:啟動時候根據實體類生成表,sessionFactory關閉時表會被刪除
#update:啟動時會根據實體類生成表,當實體類屬性表動時,表的結構也會更新,在初期開發階段使用此選項
#validate:啟動是驗證實體類和資料表是否一致,在資料結構穩定時採用此選項
#none:不採取任何手段
spring.jpa.hibernate.ddl-auto=update
#用來設定Hibernate操作的時候在控制檯顯示真實的sql語句。
spring.jpa.show-sql=true


spring.jackson.serialization.indent_output=true

debug=true

二 實體類

package com.wisely.ch8_3.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

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


}

三 實體類的Repository

package com.wisely.ch8_3.dao;

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;

import com.wisely.ch8_3.domain.Person;

@RepositoryRestResource(path = "person")
public interface PersonRepository extends JpaRepository<Person, Long> {
    
    @RestResource(path = "nameStartsWith", rel = "nameStartsWith")
    Person findByNameStartsWith(@Param("name")String name);

}

四 測試

1 輸入localhost:8080

2 列表測試,輸入localhost:8080/person

3 獲取單個物件測試,輸入localhost:8080/person/1

4 查詢測試,輸入localhost:8080/person/search/nameStartsWith?name=汪

5 分頁測試,輸入localhost:8080/person/?page=1&size=2

6 排序測試,輸入localhost:8080/person/?sort=age,desc

7 儲存測試

localhost:8080/person發起POST請求,我們將要儲存的資料放置在請求體中,資料型別設定為JSON,JSON內容如下

8 更新測試

現在我們更新新增id為7的資料,用PUT方式訪問localhost:8080/person/7,並修改提交的資料

9 刪除測試

刪除剛才新增的id=7的資料,使用DELETE方式訪問localhost:8080/person/7

10 定製根路徑

在application.properties中新增

spring.data.rest.base-path= /api

重啟後,訪問的根路徑發生了變化

11 定製節點路徑

@RepositoryRestResource(path = "people")    //定製節點路徑
public interface PersonRepository extends JpaRepository<Person, Long> {
     //Person findByNameStartsWith(String name);
     @RestResource(path = "nameStartsWith", rel = "nameStartsWith")
     Person findByNameStartsWith(@Param("name")String name);
}

測試結果如下:

如果去掉定製節點那一行,訪問時,節點使用persons路徑,也就是在實體類之後加“s”來形成路徑。

public interface PersonRepository extends JpaRepository<Person, Long> {
     //Person findByNameStartsWith(String name);
     @RestResource(path = "nameStartsWith", rel = "nameStartsWith")
     Person findByNameStartsWith(@Param("name")String name);
}

測試結果如下: