Jpa 和 Rest

maven 依赖

<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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<!-- db -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
package com.example.entity;

import lombok.Data;

import javax.persistence.Entity;

/**
 * @author 郑查磊
 * @date 2019/3/25 14:15
 * @email <a href="mailto:stone981023@gmail.com">stone</a>
 */
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

}
import com.example.entity.Person;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

/**
 * @author 郑查磊
 */
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

    List<Person> findByLastName(@Param("name") String name);

}

接着启动 就没了

然后看path people

这里借助一下工具 postman

在这里先思考一下Rest 这种风格 的CRUD

get请求 http://localhost:8080/people 查询

接口查询 http://localhost:8080/people/search/findByLastName?name=Baggins

delete` http://localhost:8080/people/1 删除

put 新增 http://localhost:8080/people/1 {"firstName": "Bilbo", "lastName": "Baggins"} 在这里 的这个json数据 选择postman中的body raw 放进去 然后选择json 如下图

AtGoWR.png

修改暂时没找到 哈哈哈 待补充 好像没啥用 逻辑还是jpa的逻辑