Springboot編寫CRUD時(shí)訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的問(wèn)題及解決方法
1. 我遇到了什么問(wèn)題
我在學(xué)習(xí)springboot,其中在編寫CRUD時(shí)發(fā)現(xiàn)訪問(wèn)數(shù)據(jù)的函數(shù)執(zhí)行下去返回值是null但是其它部分正常。
下面是我的錯(cuò)誤代碼
pojo
public class Bot {
@TableId(type = IdType.AUTO )
private Integer id ;
private Integer user_id ;
private String name ;
private String description ;
private String content ;
private Integer rating ;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date create_time ;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date modify_time ;
}數(shù)據(jù)庫(kù)列名

其中注意我是在面臨訪問(wèn)user_id這個(gè)類時(shí)出現(xiàn)了返回null。當(dāng)時(shí)目的是為了pojo和數(shù)據(jù)庫(kù)對(duì)應(yīng)。
Service
@Service
public class RemoveServiceImpl implements RemoveService {
@Autowired
BotMapper botMapper ;
@Override
public Map<String, String> remove(Map<String, String> data) {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication() ;
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal() ;
User user = loginUser.getUser() ;
Map<String,String> map = new HashMap<>();
int bot_id = Integer.parseInt(data.get("bot_id")) ;
Bot bot = botMapper.selectById(bot_id) ;
if(bot == null) {
map.put("error_message", "Bot不存在") ;
return map ;
}
System.out.println("new BOT_ID" + bot.getId());
System.out.println(bot.getName());
System.out.println(bot.getUser_id());
System.out.println(user.getId());
if(!bot.getUser_id().equals(user.getId())) {
map.put("error_message", "你沒(méi)有權(quán)限") ;
return map ;
}
botMapper.deleteById(bot_id) ;
map.put("error_message", "success") ;
return map ;
}
}其中各類訪問(wèn)數(shù)據(jù)庫(kù)的函數(shù)都是idea自動(dòng)填充的
問(wèn)題就是當(dāng)我程序進(jìn)行到這個(gè)頁(yè)面時(shí),bot.getUser_id()返回值是null其它值都是正確的
2. 我是怎么做得
后面發(fā)現(xiàn)pojo層的命名和數(shù)據(jù)庫(kù)之間要使用駝峰命名法進(jìn)行對(duì)應(yīng),關(guān)于駝峰命名法希望大家自己去查一查,因?yàn)槲乙膊皇臁5菍?duì)于數(shù)據(jù)庫(kù)中的user_id列命名需要把_變?yōu)榇髮憽?br />將pojo層變?yōu)?/p>
public class Bot {
@TableId(type = IdType.AUTO )
private Integer id ;
private Integer userId ;
private String name ;
private String description ;
private String content ;
private Integer rating ;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date create_time ;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date modify_time ;
}同時(shí)把service中的
bot.getUser_id()
改為
bot.getUserId()
問(wèn)題就解決了
到此這篇關(guān)于Springboot在編寫CRUD時(shí),訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的文章就介紹到這了,更多相關(guān)Springboot編寫CRUD返回null內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot整合Spring Security簡(jiǎn)單實(shí)現(xiàn)登入登出從零搭建教程
這篇文章主要給大家介紹了關(guān)于Spring Boot整合Spring Security簡(jiǎn)單實(shí)現(xiàn)登入登出從零搭建的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-09-09
Mybatis + js 實(shí)現(xiàn)下拉列表二級(jí)聯(lián)動(dòng)效果
這篇文章給大家介紹基于Mybatis + js 實(shí)現(xiàn)下拉列表二級(jí)聯(lián)動(dòng)效果,實(shí)現(xiàn)代碼分為前端界面實(shí)現(xiàn)和后端處理方法,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06
springboot的異步任務(wù):無(wú)返回值和有返回值問(wèn)題
這篇文章主要介紹了springboot的異步任務(wù):無(wú)返回值和有返回值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot整合Jackson超詳細(xì)用法(附Jackson工具類)
這篇文章主要介紹了SpringBoot整合Jackson超詳細(xì)教程,本篇講的是Jackson的詳細(xì)用法,Jackson工具類在文章最后,直接復(fù)制粘貼即可使用,需要的朋友可以參考下2023-03-03
解決IntelliJ?IDEA輸出中文顯示為問(wèn)號(hào)問(wèn)題的有效方法
最近剛學(xué)到文件字節(jié)流這里,但輸出中文時(shí),出現(xiàn)了控制臺(tái)輸出問(wèn)號(hào)的情況,所以下面這篇文章主要給大家介紹了關(guān)于如何解決IntelliJ?IDEA輸出中文顯示為問(wèn)號(hào)問(wèn)題的有效方法,需要的朋友可以參考下2022-07-07
Java 對(duì) Cookie增刪改查的實(shí)現(xiàn)示例
這篇文章主要介紹了Java 對(duì) Cookie增刪改查的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Spring Security整合Oauth2實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Spring Security整合Oauth2實(shí)現(xiàn)流程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

