当前位置: 首页 > news >正文

【jpa】springboot使用jpa示例

目录

          • 1. 请求示例
          • 2. pom依赖
          • 3. application.yaml
          • 4.controller
          • 5. service
          • 6. repository
          • 7. 实体
          • 8. 启动类

1. 请求示例
curl --location --request POST 'http://127.0.0.1:8080/user' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive' \
--data-raw '{"name": "张三","age": 22,"phone": "18888888888","email": "18888888888@163.com","address": "上海市嘉定区"
}'
curl --location --request GET 'http://127.0.0.1:8080/user/1' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive'
curl --location --request POST 'http://127.0.0.1:8080/user' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive' \
--data-raw '{"id": 1,"name": "李四","age": 22,"phone": "18888888888","email": "18888888888@163.com","address": "上海市嘉定区"
}'
curl --location --request DELETE 'http://127.0.0.1:8080/user/1' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive'
curl --location --request GET 'http://127.0.0.1:8080/user/list' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive'
2. pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Learn</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.5.6</version></dependency><!-- Spring Boot Starter Data JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>2.5.6</version></dependency><!-- MySQL 8.0 Connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version> <!-- 替换为具体的8.0版本,如8.0.32 --></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.34</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.52</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.5.6</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
3. application.yaml
spring:datasource:url: jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverjpa:hibernate:ddl-auto: updateshow-sql: trueproperties:hibernate:dialect: org.hibernate.dialect.MySQL8Dialect
4.controller
package com.learn.controller;import com.learn.entity.UserEntity;
import com.learn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import javax.validation.Valid;
import java.util.List;@RestController
@RequestMapping("/user")
@Valid
public class UserController {@Autowiredprivate UserService userService;@PostMappingpublic UserEntity save(@Validated @RequestBody UserEntity user) {return userService.save(user);}@DeleteMapping("/{id}")public void delete(@PathVariable Long id) {userService.delete(id);}@GetMapping("/{id}")public UserEntity findById(@PathVariable Long id) {return userService.findById(id);}@GetMapping("/list")public List<UserEntity> list() {return userService.list();}
}
5. service
package com.learn.service;import com.learn.entity.UserEntity;
import com.learn.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public List<UserEntity> list() {return userRepository.findAll();}public UserEntity save(UserEntity user) {return userRepository.saveAndFlush(user);}public void delete(Long id) {userRepository.deleteById(id);}public UserEntity findById(Long id) {return userRepository.findById(id).get();}}
6. repository
package com.learn.repository;import com.learn.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<UserEntity, Long> {
}
7. 实体
package com.learn.entity;import lombok.Data;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;/*** @date 2024/12/12* @description 实体*/
@Data
@Table(name = "user")
@Entity
@Valid
public class UserEntity {@Id@GeneratedValueprivate Long id;@NotBlank(message = "姓名不能为空")private String name;@Min(value = 0, message = "年龄不能小于0")private Integer age;@Pattern(regexp = "^1[3456789]\\d{9}$", message = "手机号格式不正确")private String phone;@Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", message = "邮箱格式不正确")private String email;@Size(max = 100, message = "地址长度不能超过100")private String address;
}
8. 启动类
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @date 2024/12/11* @description*/
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
http://www.lryc.cn/news/505067.html

相关文章:

  • Python Flask Web框架快速入门
  • ansible自动化运维(五)roles角色管理
  • 前端学习一
  • 【OSS】php使用oss存储
  • UE5 C+、C++、C# 构造方法区别示例
  • leetcode-146.LRU缓存(易理解)
  • JavaSe部分总结
  • iPhone批量删除照片的方法
  • 红日靶场vulnstack 7靶机的测试报告[细节](一)
  • ubuntu+ros新手笔记(二):古月·ROS2入门21讲学习笔记
  • Harmonyos之深浅模式适配
  • 牛客网 SQL2查询多列
  • Angular由一个bug说起之十二:网页页面持续占用CPU过高
  • 【从零开始入门unity游戏开发之——C#篇05】转义字符、@处理多行文本或者不使用转义字符、随机数
  • 我们来对接蓝凌OA --报文格式
  • 旅游系统旅游小程序PHP+Uniapp
  • Pytest-Bdd-Playwright 系列教程(15):背景(Background)
  • ionic V6 安装ios所需
  • 3d模型展示-初探
  • OpenLinkSaas 2025年1月开发计划
  • C# 用封装dll 调用c++ dll 使用winapi
  • XML基础学习
  • Jmeter直连数据库,jar包下载
  • Unity读取、新建Excel表格
  • 智能高效的IDE GoLand v2024.3全新发布——支持最新Go语言
  • OpenCV相机标定与3D重建(21)投影矩阵分解函数decomposeProjectionMatrix()的使用
  • Flink State面试题和参考答案-(下)
  • 111.【C语言】数据结构之二叉树的销毁函数
  • [论文阅读] |智能体长期记忆与反思
  • 【Trouble Shooting】Oracle ADG hung,出现ORA-04021