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 整合 ...
随机推荐
- ios端position为fixed失效的解决办法
关键代码 document.getElementById("searchInputbox").addEventListener('touchmove', handler, {pas ...
- redis-cli 通过管道 --pipe 快速导入数据到redis中
最近有个需求,需要把五千万条数据批量写入redis中,方法倒是有很多种!效率最高的就是通过redis-cl管道的方式写入 一:先看看命令 cat redis.txt | redis-cli -h 12 ...
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- socket(TCP)通讯之Python实现
1.Service address = ('localhost', 9102) # AF_INET = ipv4; SOCK_STREAM:TCP s = socket.socket(socket.A ...
- WebApi返回类型设置为json的三种方法
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- C# bool.tryparse
才工作时候是做C++的,受这个影响一直以为C# 转换 “0” 和 "false"会转换为 false,“1”和"true"转换为true,原来只有“true”才 ...
- Spring rabbitMq 中 correlationId或CorrelationIdString 消费者获取为null的问题
问题 在用Spring boot 的 spring-boot-starter-amqp 快速启动 rabbitMq 是遇到了个坑 消费者端获取不到:correlationId或Correlatio ...
- mysql数据库 删除某几个字段相同的重复记录并根据另一字段留下一条记录
1.例如Mysql数据库中表a中的记录,id=2,id=6,id=7的记录是重复的(iId,cId等多个字段相同),现在想留下id最小(id=2)或最大(id=7)的一条记录
- Java(18) 集合框架
一.集合框架 Collectoin Map List set HashMap ArrayList LinkedList ...
- 提取 linux 文件目录结构
提取 linux 文件的目录结构 find /home/user1/ -type d |while read line ;do mkdir -p /home/user2/$line;done