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的更多相关文章

  1. Spring Boot (五)Spring Data JPA 操作 MySQL 8

    一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...

  2. 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 ...

  3. Spring Boot 2.X 如何快速整合jpa?

    本文目录 一.JPA介绍二.Spring Data JPA类结构图1.类的结构关系图三.代码实现1.添加对应的Starter2.添加连接数据库的配置3.主要代码 一.JPA介绍 JPA是Java Pe ...

  4. Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...

  5. Spring 5.x 、Spring Boot 2.x 、Spring Cloud 与常用技术栈整合

    项目 GitHub 地址:https://github.com/heibaiying/spring-samples-for-all 版本说明: Spring: 5.1.3.RELEASE Spring ...

  6. spring boot 系列之五:spring boot 通过devtools进行热部署

    前面已经分享过四篇随笔: spring boot 系列之一:spring boot 入门 spring boot 系列之二:spring boot 如何修改默认端口号和contextpath spri ...

  7. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  8. Spring Boot 2(一):Spring Boot 2.0新特性

    Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...

  9. Spring Boot配置篇(基于Spring Boot 2.0系列)

    1:概述 SpringBoot支持外部化配置,配置文件格式如下所示: properties files yaml files environment variables command-line ar ...

  10. 【自学Spring Boot】什么是Spring Boot

    为啥要有Spring Boot? 以前大学刚开始学java web的时候,需要搭建起web框架,当时使用的是SSH(struts+spring+hibernate),那就开始搭建吧,初学者哪里知道整套 ...

随机推荐

  1. 【LA3461】Leonardo的笔记本

    白书例题. #include<bits/stdc++.h> using namespace std; ],vis[],cnt[]; inline int read(){ ,x=;char ...

  2. 【bzoj2242】计算器

    #include<bits/stdc++.h> #define inf 1000000000 using namespace std; typedef long long ll; ?a:g ...

  3. phoronix-test-suite测试云服务器

    centos系统 phoronix-test-suite是目前Linux下比较常用的性能测试软件. 使用phoronix-test-suite条件前提:需要安装php5,需要PHP都DOM扩展 因为是 ...

  4. PHP文件环境搭建—EcShop环境搭建

    1  rpm -qa|grep yum|xargs rpm -e --nodeps    2  ls    3  rpm -ivh python-iniparse-0.3.1-2.1.el6.noar ...

  5. Java中的标记接口(zz)

    1.什么是标记接口? Java中把没有定义任何方法和常量的接口称之为标记接口,我们经常使用的比较多的是“”Serializable“”,这个接口也是没有定义人任何方法和常量的. 2.标记接口的作用? ...

  6. JAVA MAC 配置

    1下载对应的JDK,并安装 查看是否成功 java -version 2配置环境变量 sudo vim /etc/profile 入一下内容: JAVA_HOME="/Library/Jav ...

  7. AC日记——Is it rated? codeforces 807a

    Is it rated? 思路: 水题: 代码: #include <cstdio> #include <cstring> using namespace std; ],b[] ...

  8. 转:Laravel 安装指南

    Git 介绍 之所以要说 Git,就是因为 Composre 有时需要用到 Git,还是安装上比较好,Composer 暂且不表,先来了解一下 Git 吧(已经安装的童鞋跳过这里,直接看 Compos ...

  9. CF 862A Mahmoud and Ehab and the MEX【数组操作】

    A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 megabytes in ...

  10. [Contest20171102]简单数据结构题

    给一棵$n$个点的数,点权开始为$0$,有$q$次操作,每次操作选择一个点,把周围一圈点点权$+1$,在该操作后你需要输出当前周围一圈点点权的异或和. 由于输出量较大,设第$i$个询问输出为$ans_ ...