前言 网上都在调侃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 服务。
示例 先通过一个例子看看,只需要两个类:
1 2 3 4 5 6 7 8 9 @Data @Entity public  class  User  {    @Id      @GeneratedValue      private  Long id;     private  String name;     private  Integer age; } 
1 2 public  interface  UserRepository  extends  JpaRepository <User,Long> {} 
就这样User表相关的REST接口就已经生成好了,来测试下看看。
1 2 3 4 5 6 7 8 9 10 11 12 $ curl localhost:8080 {   "_links"  : {     "users"  : {       "href"  : "http://localhost:8080/users{?page,size,sort}" ,       "templated"  : true      },     "profile"  : {       "href"  : "http://localhost:8080/profile"      }   } } 
根据上面的响应,可以看到User资源对应的接口地址,接着继续测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 $ 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"      }   } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ 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   } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ 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   } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 $ 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"      }   } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 $ 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"      }   } } 
1 $ curl -i -X DELETE localhost:8080/users/1 
核心代码只有十几行就完成了一个基本的CRUD功能,在开发小项目的时候效率非常的高,但是由于屏蔽了controller层,如果有基于拦截器或者AOP做一些定制化的功能就比较麻烦了,例如:日志审计、权限校验之类的。
未完待续