# 做个优雅的CRUD boy
4 min read
文章目录
前言
网上都在调侃CRUD没有技术含量,但是不可否认的是在工作中无可避免的要做大量的CRUD,这里面会存在大量的重复工作,意味着可能会写大量的冗余代码,秉着能少写一行代码绝不多写一行的原则,不应该把时间浪费在这些重复的工作中的,在这里分享两个方案来用尽量少的代码实现CRUD,一个是spring-data-rest还有一个是我自己封装的一套框架monkey-spring-boot-starter,下面一一进行介绍。
spring-data-rest
简介
Spring Data REST 是 Spring Data 项目的一部分,可轻松在 Spring Data repository 上构建 REST 服务,目前支持JPA、MongoDB、Neo4j、Solr、Cassandra、Gemfire,只需要定义一个repository就可以自动转换成 REST 服务。
示例
先通过一个例子看看,只需要两个类:
- User.java
@Data@Entitypublic class User { @Id @GeneratedValue private Long id; private String name; private Integer age;}- UserRepository.java
public interface UserRepository extends JpaRepository<User,Long> {}就这样User表相关的REST接口就已经生成好了,来测试下看看。
- 访问根目录,会列出所有可用的资源列表
$ curl localhost:8080{ "_links" : { "users" : { "href" : "http://localhost:8080/users{?page,size,sort}", "templated" : true }, "profile" : { "href" : "http://localhost:8080/profile" } }}根据上面的响应,可以看到User资源对应的接口地址,接着继续测试。
- 添加用户
$ curl -X POST -i -H "Content-Type:application/json" -d '{"name":"lee","age":18}' localhost:8080/users{ "name" : "lee", "age" : 18, "_links" : { "self" : { "href" : "http://localhost:8080/users/1" }, "user" : { "href" : "http://localhost:8080/users/1" } }}- 查询用户
$ curl localhost:8080/users{ "_embedded" : { "users" : [ { "name" : "lee", "age" : 18, "_links" : { "self" : { "href" : "http://localhost:8080/users/1" }, "user" : { "href" : "http://localhost:8080/users/1" } } } ] }, "_links" : { "self" : { "href" : "http://localhost:8080/users" }, "profile" : { "href" : "http://localhost:8080/profile/users" } }, "page" : { "size" : 20, "totalElements" : 1, "totalPages" : 1, "number" : 0 }}- 分页查询用户
$ curl "localhost:8080/users?page=0&size=10"{ "_embedded" : { "users" : [ { "name" : "lee", "age" : 18, "_links" : { "self" : { "href" : "http://localhost:8080/users/1" }, "user" : { "href" : "http://localhost:8080/users/1" } } } ] }, "_links" : { "self" : { "href" : "http://localhost:8080/users?page=0&size=10" }, "profile" : { "href" : "http://localhost:8080/profile/users" } }, "page" : { "size" : 10, "totalElements" : 1, "totalPages" : 1, "number" : 0 }}- 修改用户
$ curl -X PUT -H "Content-Type:application/json" -d '{"name":"hello","age":20}' localhost:8080/users/1{ "name" : "hello", "age" : 20, "_links" : { "self" : { "href" : "http://localhost:8080/users/1" }, "user" : { "href" : "http://localhost:8080/users/1" } }}$ curl -X PATCH -H "Content-Type:application/json" -d '{"age":18}' localhost:8080/users/1{ "name" : "hello", "age" : 18, "_links" : { "self" : { "href" : "http://localhost:8080/users/1" }, "user" : { "href" : "http://localhost:8080/users/1" } }}- 删除用户
$ curl -i -X DELETE localhost:8080/users/1核心代码只有十几行就完成了一个基本的CRUD功能,在开发小项目的时候效率非常的高,但是由于屏蔽了controller层,如果有基于拦截器或者AOP做一些定制化的功能就比较麻烦了,例如:日志审计、权限校验之类的。