[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),那就开始搭建吧,初学者哪里知道整套 ...
随机推荐
- Java坦克大战 (六) 之增加可玩性
本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...
- hdu5819
补多校系列,具体见多校题解http://www.cnblogs.com/duoxiao/p/5777700.html 值得注意的是如果当前i初始向左,前i个骑士最终只有1个向右 对于f[i][1]状态 ...
- python带cookie提交表单自动登录
import urllib import urllib2 import cookielib login_url = "xxxxxxxxxxxxx" cj = cookielib.C ...
- #!bin/sh是啥
第一句的#!是对脚本的解释器程序路径,脚本的内容是由解释器解释的,我们可以用各种各样的解释器来写对应的脚本,比如说/bin/csh脚本,/bin/perl脚本,/bin/awk脚本,/bin/sed脚 ...
- php中parse_url函数的源码及分析
前言 看师傅们的文章时发现,parse_url出现的次数较多,单纯parse_url解析漏洞的考题也有很多,在此研究一下源码(太菜了看不懂,待日后再补充Orz) 源码 PHPAPI php_url * ...
- 形态学函数cvMorphologyEx
OpenCV提供了通用的形态学函数cvMorphologyEx,该函数能够实现开运算,闭运算,形态梯度,礼帽操作,黑帽操作 接口形式 编辑 void cvMorphologyEx(const CvAr ...
- CF 1005A Tanya and Stairways 【STL】
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairw ...
- 10、Flask实战第10天:视图使用装饰器
在实际开发中,我们有时候会用到自己定义装饰器并应用到函数视图或者类视图里面:比如:我们要想进入个人中心页面,首先要验证你是否登录,否则进不去,下面我们来模拟这个场景 定义一个装饰器 from func ...
- JavaScript中的局部作用域及常量的定义
局部作用域 通常JavaScript的作用域是函数内部,在类似for循环的语句块中是无法申明局部变量的. function exm() { for (var i=0; i<100; i++) { ...
- 【线性基】【贪心】【独立环】bzoj2115 [Wc2011] Xor
网上到处都是题解,自己画个图也很好理解.虽然环的个数很多,但是都可以通过独立环之间异或出来,不用管. 独立环求法:生成树之后,每次向图里添加非树边(u,v),则这个独立环的异或和为sum[u]^sum ...