国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用springboot實(shí)現(xiàn)查詢更改數(shù)據(jù)庫(kù)需求

這篇具有很好參考價(jià)值的文章主要介紹了使用springboot實(shí)現(xiàn)查詢更改數(shù)據(jù)庫(kù)需求。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

使用springboot實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)庫(kù)數(shù)據(jù)交互請(qǐng)求

實(shí)現(xiàn):通過(guò)springboot框架,實(shí)現(xiàn)接口

  1. /user/view;

查詢數(shù)據(jù)庫(kù)user表中的user數(shù)據(jù),

  1. /user/insert;

新增user數(shù)據(jù)到user表

  1. /user/update

修改user的對(duì)應(yīng)user信息

  1. 集成規(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)邏輯

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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包