[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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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