使用spring REST可以对单表资源操作不需要Controller层的代码,甚至不使用任何实现,使用方便暴力!快速开发可以考虑
1,引入maven
(有些资料说还需要单独引入spring-boot-starter-data-rest,但是我看到源码已经在autoConfigure中了,其他依赖就不需要了,但是还是需要引入jpa)
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
2,在yml中配置下前置(路径)
3,写entiry实体类、jpa注解和JpaRepository
package cn.newtouch.step.code.entity;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Created by Administrator on 2016/11/8.
*/
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Test {
@Id
private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String 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;
}
}
package cn.newtouch.step.code.repository;
import cn.newtouch.step.code.entity.Test;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
/**
* Created by Administrator on 2016/11/8.
*/
//@Component
public interface TestRepository extends JpaRepository<Test,String>{
@RestResource(path = "name",rel = "name")
Test findByName(@Param("name") String name);
}
也可以修改默认加s的路径
效果如图: