[Spring Boot] Adding JPA and Spring Data JPA
JPA is just like a helper class for providing data for Controller, has method like 'findOne', 'findAll', 'saveAndFlush', 'delete'.
in repository/ShipwreckRespository.java:
package hello.respository; import org.springframework.data.jpa.repository.JpaRepository;
import hello.model.Shipwreck; public class ShipwreckRespository extends JpaRepository<Shipwreck, Long>{ }
in model/Shipwreck.java:
package hello.model; import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue; @Entity
public class Shipwreck {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String description;
String condition;
Integer depth;
Double latitude;
Double longitude;
Integer yearDiscovered;
controller:
@RestController
@RequestMapping("api/v1/")
public class ShipController { @Autowired
private ShipwreckRespository shipwreckRespository; @RequestMapping(value="shipwrecks", method= RequestMethod.GET)
public List <Shipwreck>list() {
return shipwreckRespository.findAll();
} @RequestMapping(value = "shipwrecks", method = RequestMethod.POST)
public Shipwreck create(@RequestBody Shipwreck shipwreck) {
return shipwreckRespository.saveAndFlush(shipwreck);
} @RequestMapping(value="shipwrecks/{id}", method = RequestMethod.GET)
public Shipwreck get(@PathVariable long id) {
return shipwreckRespository.findOne(id);
} @RequestMapping(value="shipwrecks/{id}", method = RequestMethod.PUT)
public Shipwreck update(@PathVariable long id, @RequestBody Shipwreck shipwreck) {
Shipwreck shipwreckExisting = shipwreckRespository.findOne(id);
BeanUtil.copyProperties(shipwreck, shipwreckExisting);
return shipwreckRespository.saveAndFlush(shipwreckExisting);
} @RequestMapping(value="shipwrecks/{id}", method = RequestMethod.DELETE)
public Shipwreck delete(@PathVariable long id) { Shipwreck shipwreckExisting = shipwreckRespository.findOne(id);
shipwreckRespository.delete(shipwreckExisting);
return shipwreckExisting;
}
}
[Spring Boot] Adding JPA and Spring Data JPA的更多相关文章
- Spring Boot (五)Spring Data JPA 操作 MySQL 8
一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...
- Spring Boot 2.x 之 Spring Data JPA, Hibernate 5
1. Spring Boot常用配置项 基于Spring Boot 2.0.6.RELEASE 1.1 配置属性类 spring.jpa前缀的相关配置项定义在JpaProperties类中, 1.2 ...
- Spring Boot 2.X 如何快速整合jpa?
本文目录 一.JPA介绍二.Spring Data JPA类结构图1.类的结构关系图三.代码实现1.添加对应的Starter2.添加连接数据库的配置3.主要代码 一.JPA介绍 JPA是Java Pe ...
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- Spring 5.x 、Spring Boot 2.x 、Spring Cloud 与常用技术栈整合
项目 GitHub 地址:https://github.com/heibaiying/spring-samples-for-all 版本说明: Spring: 5.1.3.RELEASE Spring ...
- spring boot 系列之五:spring boot 通过devtools进行热部署
前面已经分享过四篇随笔: spring boot 系列之一:spring boot 入门 spring boot 系列之二:spring boot 如何修改默认端口号和contextpath spri ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
- Spring Boot 2(一):Spring Boot 2.0新特性
Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...
- Spring Boot配置篇(基于Spring Boot 2.0系列)
1:概述 SpringBoot支持外部化配置,配置文件格式如下所示: properties files yaml files environment variables command-line ar ...
- 【自学Spring Boot】什么是Spring Boot
为啥要有Spring Boot? 以前大学刚开始学java web的时候,需要搭建起web框架,当时使用的是SSH(struts+spring+hibernate),那就开始搭建吧,初学者哪里知道整套 ...
随机推荐
- php编译中遇到种种error解决办法
http://my.oschina.net/maczhao/blog/365176 编译PHP5.5 make 时出现错误 make: *** [ext/fileinfo/libmagic/appre ...
- golang中的反射
反射操作普通变量 package main import ( "fmt" "reflect" ) func main(){ a := 1 //reflect.T ...
- 【 总结 】crontab 使用脚本及直接获取HTTP状态码
一.在crontab里面计划执行的脚本,所有的命令都要写出绝对路径.因为crontab的独立的进程,可能无法直接加载环境变量. 二.在判断网站能否正常访问一般的思路: 1. 判断网站是否能够正常打开. ...
- jQuery插件--zTree中点击节点实现页面跳转时弹出两个页面的问题
这是第一次使用zTree,所以在使用之前我要先写一个demo来学习一下.我们要注意的是,zTree是一个jQuery插件,所以我们在导入zTree的js文件之前要先导入jQuery的js文件. 我们先 ...
- DRF基类APIView的子类GenericAPIView
DRF的基类是APIView类,GenericAPIView类是APIView类的子类. GenericAPIView类有什么存在的意义呢? 其实, 他主要提供了两个用处: 1.提供关于数据库查询的属 ...
- Django REST Framework的序列化器是什么?
# 转载请留言联系 用Django开发RESTful风格的API存在着很多重复的步骤.详细可见:https://www.cnblogs.com/chichung/p/9933861.html 过程往往 ...
- inside a shard
fsync sync fsync/syncsync is a standard system call in the Unix operating system, which commits to d ...
- k8s通过configmap管理应用配置信息
Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...
- hdu5081
题意有点绕,不过读懂了之后并不难 以Si结尾容易想到ac自动机,建好ac自动机并将fail指针反向即可得到一棵树 那么操作1就是将若干个子树的并中的节点全部权值+1 操作2就是将求若干个节点到根的路径 ...
- 统计mysql库中每张表的行数据
修改数据库配置文件:vim /etc/my.cnf [client] user=username password=password 使用shell脚本统计表中的行数据:count.sh #!/bin ...