1. 创建一个Spring工程

2.配置application文件

spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=

3.创建实体类Piano

1.定义好属性并设置setget方法

2.添加@Entity && @Table(name = "piano")注解

3.添加@Id && @GeneratedValue(strategy = GenerationType.IDENTITY)

4.连接数据库

完成

package com.test.piano.entity;import javax.persistence.*;@Entity
@Table(name = piano)
public class piano {private int id;private String brand;private String price;private String pic;@Id@GeneratedValue(strategy = GenerationType.IDENTITY)public int getId() {return id;}public void setId(int id) {this.id = id;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}public String getPic() {return pic;}public void setPic(String pic) {this.pic = pic;}
}

5.创建Dao和Service的包

6.在Dao中创建一个接口并继承JpaRepository,然后指定实体类piano和主键的类型Integer

public interface pianoRepository extends JpaRepository<piano,Integer>

7.在Service中创建类PianoService用于实现增删改查

8.添加注解

@Service表示是业务逻辑层
@Transactional(readOnly = false)
readOnly=true表明所注解的方法或类只是读取数据。
readOnly=false表明所注解的方法或类是增加,删除,修改数据。

4.创建Controller类

package com.test.piano.controller;import com.test.piano.entity.piano;
import com.test.piano.service.PianoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;/*** @date 2020/3/24* @author Charlotte*/@RestController
@RequestMapping("/piano")
public class PianoController {@Resourceprivate PianoService pianoService;@RequestMapping("/hello")public String hello(){return "Hello World!";}@RequestMapping("/add")public String add(){return "添加成功!";}@RequestMapping("/list")public List<piano> findAll(){return this.pianoService.findAll();}
}

Spring JPA实现增删改查相关推荐

  1. Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    <p>这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例.</p> 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭 ...

  2. (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...

  3. Spring Date JPA实现增删改查

    1.新建一个Cart类 package com.entity;public class Cart {private int id;private int userId;private int medi ...

  4. java--spring5之spring实现jdbc增删改查----4

    1.引入依赖: aspectjweaver-1.6.8.jar cglib-2.2.0.jar.zip commons-logging-1.1.1.jar druid-1.1.9.jar hamcre ...

  5. Spring Data ElasticSearch增删改查

    Spring Data ElasticSearch 一.介绍 ElasticSearch 学习路线图 1.学习ElasticSearch安装及其API操作 2. 学习原生ElastricSearch ...

  6. Spring JdbcTemplate CRUD增删改查操作

    执行insert update delete操作 只需要使用JdbcTemplate的update方法就可以执行insert update delete操作. @RunWith(SpringJUnit ...

  7. spring入门(11)-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)

    今天是spring的最后一节课,这节课老师讲了spring与hibernate整合完成增删改查的操作,这是很重要的一节课,这也是第一次真正的实现spring结合Hibernate和数据库连接上,下面是 ...

  8. 用Spring+jdbc 写一个数据库增删改查

    用Spring+Jdbc写一个数据库增删改查 先展示项目结构 ClassBiz.java package com.gxj.biz;import java.util.List;import com.gx ...

  9. spring boot(十五)spring boot+thymeleaf+jpa增删改查示例

    快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency><groupId>org.springframework.bo ...

最新文章

  1. Flutter 配置
  2. 【iOS工具】rvm、Ruby环境和CocoaPods安装使用及相关报错问题解决(2016 12 15 更新)...
  3. ITK:遮罩图像的归一化相关性
  4. 语音跟踪:信号分解、锁相、鸡尾酒会效应、基于PR的信号分离
  5. python如何读取文件夹下的子文件夹
  6. springboot2 使用hikaridatasource 并测试_基于Spring Boot 2.x的后端管理网站脚手,源码免费分享...
  7. 【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)
  8. C++ 泛型模板进阶
  9. PMP考试的一点学习感悟
  10. 苏宁11.11:苏宁易购移动端的架构优化实践
  11. 项目经理(甲方)推行ERP,需要做些什么?
  12. 软件测试工程师的职业规划
  13. 实现高效的英文单词前缀匹配
  14. 强化学习:玩转Atari-Pong游戏
  15. ERP、MES、APS在生产排程上的区别?
  16. 洛谷 P1359 租用游艇
  17. 机器学习及SparkMLlib简介
  18. SAP ABAP 自动批量开关账期程序 OB52和MMPV
  19. Gherkin简单使用
  20. 鸿蒙os电脑界面,鸿蒙OS平板UI界面曝光:双Dock栏设计+超级终端互联功能

热门文章

  1. 为VIP解决问题时写的源码
  2. Spring Cloud--鸿鹄Cloud分布式微服务云系统—云架构代码结构构建(五)
  3. 大数据将植物学研究带入新境界
  4. java 同步 set_Java Collections synchronizedSet()用法及代码示例
  5. PHPstorm如何导入字体主题
  6. 巧技拾遗 | JavaScript 中 Array.every 和 Array.map 的巧妙结合
  7. Observer设计模式中-委托事件-应用在消息在窗体上显示
  8. 【数据结构笔记42】哈希表应用:文件中单词词频统计
  9. OC-NSFileManager
  10. mysql atlas 实现读写分离分担数据库压力