[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;
    }
}
复制代码

 

posted @   Zhentiw  阅读(416)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-01-16 [Javascript] Transduce over any Iteratable Collection
2018-01-16 [Javascript] Improve Composition with the Compose Combinator
2017-01-16 [Angular Directive] Create a Template Storage Service in Angular 2
2017-01-16 [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2
2017-01-16 [Angular Directive] Implement Structural Directive Data Binding with Context in Angular
2015-01-16 [MODx] 10. Using Babel for Muti-languages support
点击右上角即可分享
微信分享提示