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. Oracle基础 09 概要文件 profile

    --创建 profile 概要文件create profile profile123 limit failed_login_attempts 2;  --修改用户的 profile 文件alter u ...

  2. BAT 前端开发面经 —— 吐血总结

    更好阅读,请移步这里 聊之前 最近暑期实习招聘已经开始,个人目前参加了阿里的内推及腾讯和百度的实习生招聘,在此总结一下 一是备忘.总结提升,二是希望给大家一些参考 其他面试及基础相关可以参考其他博文: ...

  3. django使用celery进行耗时任务的优化

    # 原创,转载请留言联系 在用django做项目的时候,做到注册模块时,需要发送短信验证码.本来简简单单的做好了,后来优化的时候发现,发送短信验证码的时候需要一点时间,在这个时间之内程序是阻塞的,用户 ...

  4. javascript三种嵌入方式

    什么是JavaScript? JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互,JavaScript是浏览器解释执行的. J ...

  5. hdu 1068(最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. 微信小程序开发参考资料汇总

    不错的微信小程序入门教程:微信小程序入门二: 条件.遍历.网络请求.获取本地图片http://blog.csdn.net/lecepin/article/details/54016701 微信小程序入 ...

  7. crontab自动备份MySQL数据库并删除5天前备份

    1.创建备份文件夹 //备份数据库文件夹 mkdir /data/backmysql //crontab日志 mkdir /data/logs   2.创建脚本文件 db_user="xxx ...

  8. flutter 国内镜像设置

    官网:https://flutter.dev/community/china 路径设置 FLUTTER_STORAGE_BASE_URL: https://mirrors.sjtug.sjtu.edu ...

  9. Python3 list记录

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author;Tsukasa name = ['YangJiaHui','LiuYueEr','TaB ...

  10. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...