使用springboot实现简单的数据库数据交互请求
实现:通过springboot框架,实现接口
-
/user/view;
查询数据库user表中的user数据,
-
/user/insert;
新增user数据到user表
-
/user/update
修改user的对应user信息
-
集成规范数据响应:
package MessageResponse;
/**
* 规范响应类
* @param <T>
*/
public class MessageResponse<T> {
private int code;
private String msg;
private T data;
//构建相应json数据
public MessageResponse(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
//成功相应
public static <T> MessageResponse<T> success(T data) {
return new MessageResponse<>(200, "ok", data);
}
//失败相应
public static <T> MessageResponse<T> error(int code, String msg) {
return new MessageResponse<T>(code, msg, null);
}
// Getters and Setters
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
功能实现:
定义简单的user类并添加对应的user表,用于数据交互
package OpportunityMatching;
import jakarta.persistence.*;
import lombok.Data;
@Data//自动生成构造函数
@Entity
@Table(name = "user")//user表
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String phone;
// 省略构造函数、getter和setter方法
}
创建数据访问对象(DAO) 创建一个名为UserRepository的接口,扩展自Spring Data JPA提供的CrudRepository。这将提供一组用于对User实体执行CRUD操作的方法
package OpportunityMatching;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
}
构造响应函数,实现查询,添加,更改数据库的响应逻辑文章来源:https://www.toymoban.com/news/detail-571054.html
package OpportunityMatching;
import MessageResponse.MessageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class OpportunityMatching {
@Autowired
private UserRepository userRepository;
@GetMapping("/view")
public MessageResponse<Iterable<User>> viewUsers() {
try {
Iterable<User> users = userRepository.findAll();
return MessageResponse.success(users);
} catch (Exception e) {
return MessageResponse.error(500, "Failed to retrieve users: " + e.getMessage());
}
}
@PostMapping("/insert")
public MessageResponse<User> insertUser(@RequestBody User user) {
try {
User savedUser = userRepository.save(user);
return MessageResponse.success(savedUser);
} catch (Exception e) {
return MessageResponse.error(500, "Failed to insert user: " + e.getMessage());
}
}
@PutMapping("/update/{id}")
public MessageResponse<User> updateUser(@PathVariable int id, @RequestBody User user) {
try {
user.setId(id);
User updatedUser = userRepository.save(user);
return MessageResponse.success(updatedUser);
} catch (Exception e) {
return MessageResponse.error(500, "Failed to update user: " + e.getMessage());
}
}
}
本文由 mdnice 多平台发布文章来源地址https://www.toymoban.com/news/detail-571054.html
到了这里,关于使用springboot实现查询更改数据库需求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!