spring-data-rest的魔力 10分钟实现增删改查
近日发现了spring-data-rest项目,于是创建这个spring-data-rest-glance来体验一下。
本例使用springboot,并使用了 spring-data-rest 和 spring-data-jpa
此二者结合:真的可以实现10分钟创建一个rest应用,下面开始演示spring-data-rest+spring-data-rest的魔力
本例假设你已经熟悉或者了解 springboot,spring-data-jpa
创建项目
我们先创建一个springboot项目,可以通过 start.spring.io 或者在idea里边new module --> spring Initializer 的方式。
创建好项目之后,我们的依赖配置是这样的:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
本例使用的是h2数据库,所以加了h2数据库的依赖。
我们创建一个person表,并创建person的entity和repository,让repository继承JpaRepository。
关键的来了:@RepositoryRestResource(collectionResourceRel = "person", path = "person")
我们给repository加上一个@RepositoryRestResource注解,我们来启动项目,看看此注解的魔力。
启动项目
访问:htttp://localhost:8000/person 得到的结果如下:
{
"_embedded": {
"person": []
},
"_links": {
"self": {
"href": "http://localhost:8000/person{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8000/profile/person"
},
"search": {
"href": "http://localhost:8000/person/search"
}
},
"page": {
"size": 20,
"totalElements": 0,
"totalPages": 0,
"number": 0
}
}
我们看到 person节点并无内容。同时请注意 _links 节点下的内容。我们下面将会用到它。
添加person
我们使用POST方式访问 http://localhost:8000/person 并提交如下 JSON 数据:
{"firstName": "tomcat", "lastName": "cat"}
如此,就完成了添加操作,你可以多添加几条数据试试。
查看person 及 person 列表
我们再次在浏览器中访问(GET) http://localhost:8000/person。得到的结果中,JSON数据和第一步中一样,person节点中不再是空的了。
[
{
"firstName": "tomcat",
"lastName": "cat",
"_links": {
"self": {
"href": "http://localhost:8000/person/1"
},
"person": {
"href": "http://localhost:8000/person/1"
}
}
}
]
我们可以继续多添加几条数据,方便下面展示查询。在添加多条信息之后,如果想查看某个person的详情,例如:http://localhost:8000/person/7
{
"firstName": "李",
"lastName": "四",
"_links": {
"self": {
"href": "http://localhost:8000/person/7"
},
"person": {
"href": "http://localhost:8000/person/7"
}
}
}
条件查询
假设我们需要根据用户名查询用户,我们在PersonRepository中添加一个方法findByLastName.
托spring-data-jpa的福,我们只需要写这样的一行代码,然后什么都不用做,spring-data-jpa会解析findByLastName并应用到查询上。
List<Person> findByLastName(@Param("name") String name);
写好上面的代码之后,我们重启项目,访问http://localhost:8000/person/search结果如下:
{
"_links": {
"findByLastName": {
"href": "http://localhost:8000/person/search/findByLastName{?name}",
"templated": true
},
"findByFirstName": {
"href": "http://localhost:8000/person/search/findByFirstName{?name}",
"templated": true
},
"self": {
"href": "http://localhost:8000/person/search"
}
}
}
我们可以看到,这里已经列出了当前可用的search方法。我们访问:http://localhost:8000/person/search/findByLastName?name=cat
{
"person": [
{
"firstName": "tomcat",
"lastName": "cat",
"_links": {
"self": {
"href": "http://localhost:8000/person/1"
},
"person": {
"href": "http://localhost:8000/person/1"
}
}
},
{
"firstName": "tom",
"lastName": "cat",
"_links": {
"self": {
"href": "http://localhost:8000/person/2"
},
"person": {
"href": "http://localhost:8000/person/2"
}
}
}
]
}
我们可以看到,这里通过findByLastName?name=cat找到了两个人:tomcat cat 和 tom cat
分页查询
为了演示分页,我们先多添加几条用户数据。在第一步中展示的结果中,我们可以看到这样的一行数据:
http://localhost:8000/person{?page,size,sort}
这提示了我们分页的使用方法,我们来访问http://localhost:8000/person?page=2&size=3 试试,即:访问第2页数据,页大小是3。
下面贴出 关键结果的节点:
{
"_embedded": {
"person": [
{
"firstName": "李",
"lastName": "四",
"_links": {
"self": {
"href": "http://localhost:8000/person/7"
},
"person": {
"href": "http://localhost:8000/person/7"
}
}
},
{
"firstName": "王",
"lastName": "五",
"_links": {
"self": {
"href": "http://localhost:8000/person/8"
},
"person": {
"href": "http://localhost:8000/person/8"
}
}
}
]
},
"page": {
"size": 3,
"totalElements": 8,
"totalPages": 3,
"number": 2
}
}
确实查到了数据,结果对不对呢?根据上面的从上面的结果看出,我们添加8条数据,页大小是3,所以:
总页数 = 3 第一页 3条数据 第二页 3条数据 第三页 2条数据
我们访问的是,http://localhost:8000/person?page=2&size=3page=2,但是实际上取的是2条数据——是第3页。那么页码其实是从0开始的对吗?
我们继续访问 http://localhost:8000/person?page=0&size=3 http://localhost:8000/person?page=1&size=3
可以发现,确实是如此,页码从0开始。any way
controller 去哪里了
到目前为止,我们只写了很少的代码,只写了DAO,但是却已经实现了增删改查resp api。我们甚至连 controller都没有写,就访问了这么多的rest url。
我们只通过@RepositoryRestResource(collectionResourceRel = "person", path = "person")在 dao 中就能够把 /path路径暴露出来。
边一切都有了,这就是spring-data-rest的魔力。
自定义 spring-data-rest 魔力之外的controller可以吗
当然可以了,上面我们所访问的 /person/* 的地址,是从dao中通过 @RepositoryRestResource 注解暴露出去的。
那么现在我们就手写一个controller,访问路径也叫/person,即:@RequestMapping("/person")
@Controller
@RequestMapping("/person")
public class PersonController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return " Hello,welcome to the normal controller! ";
}
}
我们自己创建的controller访问路径也是,/person 还创建了一个自定义的 hello方法,这个/person 和dao里边暴露的/person
能共存,并和谐相处吗?我们访问看看:http://localhost:8000/person/hello 我们在浏览器中可以看到:
Hello,welcome to the normal controller!
很完美,这里我们可以得出,我们能利用spring-data-rest + spring-data-jpa实现基本的增删改查api.
我们只需要自己去写复杂的api就行了,简单的根本不用写,岂不是很快!
总结
至此,我们体验了一下 spring-data-rest。总有“刁民”说java开发很慢,代码太多了。多吗?不多啊,我们这里使用spring-data-jpa
加上spring-data-rest,只写了很少的代码就实现了大部分基础的功能了。下次开发新项目,可以尝试使用 spring-data-rest加spring-data-jpa了。
推荐阅读
探索Java9 模块系统和反应流
Java8系列- 如何用Java8 Stream API找到心仪的女朋友
Java8系列- 何用Java8 Stream API进行数据抽取与收集
Spring Security 入门原理及实战
SpringMVC是怎么工作的,SpringMVC的工作原理
Mybatis Mapper接口是如何找到实现类的-源码分析
小程序云开发:菜鸟也能全栈做产品
CORS详解,CORS原理分析
工作6年,失业19天

spring-data-rest的魔力 10分钟实现增删改查的更多相关文章
- ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查
三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...
- Spring boot+mybatis+thymeleaf 实现登录注册,增删改查
本文重在实现理解,过滤器,业务,逻辑需求,样式请无视.. 项目结构如下 1.idea新建Spring boot项目,在pom中加上thymeleaf和mybatis支持.pom.xml代码如下 < ...
- Spring学习笔记:声明式事务管理增删改查业务
一.关于是事务 以方法为单位,进行事务控制:抛出异常,事务回滚. 最小的执行单位为方法.决定执行成败是通过是否抛出异常来判断的,抛出异常即执行失败 二.声明式事务: 声明式事务(declarative ...
- Spring Data CrudRepository增删改查方法(八)
CrudRepository 的主要方法 long count(); boolean exists(Integer arg0); <S extends StudentPO> S sav ...
- jeesite应用实战(数据增删改查),认真读完后10分钟就能开发一个模块
jeesite配置指南(官方文档有坑,我把坑填了!)这篇文章里,我主要把jeesite官方给出的帮助文档的坑填了,按照里面的方法可以搭建起来jeesite的站点.系统可以运行以后,就可以进入开发模块了 ...
- JS组件系列——又一款MVVM组件:Vue(一:30分钟搞定前端增删改查)
前言:关于Vue框架,好几个月之前就听说过,了解一项新技术之后,总是处于观望状态,一直在犹豫要不要系统学习下.正好最近有点空,就去官网了解了下,看上去还不错的一个组件,就抽空研究了下.最近园子里vue ...
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller //控制类 service //服务接口 service.impl //服务 ...
- sssp-springmvc+spring+spring-data-jpa增删改查
环境:IDE:eclipse.jdk1.7.mysql5.7.maven 项目结构图 上面目录结构你可以自己创建 搭建框架 首先加入maven依赖包以及相关插件 <dependencies> ...
- SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax 无刷新验证该用户是否存在)
软件152 余建强 该文将以员工.部门两表带领大家进入SSH的整合教程: 源码下载:http://download.csdn.net/detail/qq_35318576/9877235 SSH 整合 ...
随机推荐
- C# 将前端传来的图片文件分别以大图和缩略图保存
HttpPostedFile pic_upload = Request.Files["file"]; Bitmap bitmap = (Bitmap)System.Drawing. ...
- LeetCode--11_Container_With_Most_Water
题目链接:点击这里 首先我们不考虑高度的话 最大的面积应该是l r 应该是最边上的值 ,我们要取最大 所以 要维护从左到右单调增,从右到左 单调增 这样我们才能保证 面积增加 public stati ...
- 七.django模型系统(一)
Ⅰ.django的ORM 1.含义 对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语 ...
- 浅谈Kubernetes生产架构
注意本文,只是笔者针对Kubernetes生产环境运行的一些关于架构设计和实现方案的总结,内容很粗糙,同时也会不断完善. 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下 ...
- marquee标签的使用
marquee语法 <marquee></marquee> 实例一<marquee>Hello, World</marquee> marquee常 ...
- 1.9 分布式协调服务-Zookeeper(一)
前言 分布式环境的特点 分布性 并发性 程序运行过程中,并发性操作是很常见的.比如同一个分布式系统中的多个节点,同时访问一个共享资源.数据库.分布式存储 无序性 进程之间的消息通信,会出现顺序不一致问 ...
- 状态模式-State Pattern(Java实现)
状态模式-State Pattern 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. State接口 ...
- 解决beego1.12新版本没有log.info
去https://github.com/astaxie/beego/中,找到旧的版本下载其log.go 至本地beego目录中
- Java调用第三方http接口的方式
1. 概述 在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适.很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对方提供的接口或第三方接口(短信.天气等). 在J ...
- python--协程之特别篇
Python通过yield提供了对协程的基本支持,但是不完全.而第三方的gevent为Python提供了比较完善的协程支持. gevent是第三方库,通过greenlet实现协程,其基本思想是: 当一 ...