使用springboot實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)庫(kù)數(shù)據(jù)交互請(qǐng)求
實(shí)現(xiàn):通過(guò)springboot框架,實(shí)現(xiàn)接口
-
/user/view;
查詢數(shù)據(jù)庫(kù)user表中的user數(shù)據(jù),
-
/user/insert;
新增user數(shù)據(jù)到user表
-
/user/update
修改user的對(duì)應(yīng)user信息
-
集成規(guī)范數(shù)據(jù)響應(yīng):
package?MessageResponse;
/**
?*?規(guī)范響應(yīng)類
?*?@param?<T>
?*/
public?class?MessageResponse<T>?{
????private?int?code;
????private?String?msg;
????private?T?data;
????//構(gòu)建相應(yīng)json數(shù)據(jù)
????public?MessageResponse(int?code,?String?msg,?T?data)?{
????????this.code?=?code;
????????this.msg?=?msg;
????????this.data?=?data;
????}
????//成功相應(yīng)
????public?static?<T>?MessageResponse<T>?success(T?data)?{
????????return?new?MessageResponse<>(200,?"ok",?data);
????}
????//失敗相應(yīng)
????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;
????}
}
功能實(shí)現(xiàn):
定義簡(jiǎn)單的user類并添加對(duì)應(yīng)的user表,用于數(shù)據(jù)交互
package?OpportunityMatching;
import?jakarta.persistence.*;
import?lombok.Data;
@Data//自動(dòng)生成構(gòu)造函數(shù)
@Entity
@Table(name?=?"user")//user表
public?class?User?{
????@Id
????@GeneratedValue(strategy?=?GenerationType.IDENTITY)
????private?int?id;
????private?String?name;
????private?String?phone;
????//?省略構(gòu)造函數(shù)、getter和setter方法
}
創(chuàng)建數(shù)據(jù)訪問(wèn)對(duì)象(DAO) 創(chuàng)建一個(gè)名為UserRepository的接口,擴(kuò)展自Spring Data JPA提供的CrudRepository。這將提供一組用于對(duì)User實(shí)體執(zhí)行CRUD操作的方法
package?OpportunityMatching;
import?org.springframework.data.repository.CrudRepository;
public?interface?UserRepository?extends?CrudRepository<User,?Integer>?{
}
構(gòu)造響應(yīng)函數(shù),實(shí)現(xiàn)查詢,添加,更改數(shù)據(jù)庫(kù)的響應(yīng)邏輯文章來(lái)源:http://www.zghlxwxcb.cn/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 多平臺(tái)發(fā)布文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-571054.html
到了這里,關(guān)于使用springboot實(shí)現(xiàn)查詢更改數(shù)據(jù)庫(kù)需求的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!