目录
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=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
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 {
@Autowired
private UserService userService;
@PostMapping
public 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 {
@Autowired
private 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
@GeneratedValue
private 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);
}
}