Springboot編寫CRUD時(shí)訪問對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的問題及解決方法
1. 我遇到了什么問題
我在學(xué)習(xí)springboot,其中在編寫CRUD時(shí)發(fā)現(xià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ù)列名

其中注意我是在面臨訪問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", "你沒有權(quán)限") ;
return map ;
}
botMapper.deleteById(bot_id) ;
map.put("error_message", "success") ;
return map ;
}
}其中各類訪問數(shù)據(jù)庫(kù)的函數(shù)都是idea自動(dòng)填充的
問題就是當(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()
問題就解決了
到此這篇關(guān)于Springboot在編寫CRUD時(shí),訪問對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的文章就介紹到這了,更多相關(guān)Springboot編寫CRUD返回null內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Shiro框架,實(shí)現(xiàn)用戶權(quán)限管理
Apache Shiro是一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會(huì)話管理。作為一款安全框架Shiro的設(shè)計(jì)相當(dāng)巧妙。Shiro的應(yīng)用不依賴任何容器,它不僅可以在JavaEE下使用,還可以應(yīng)用在JavaSE環(huán)境中。2021-06-06
關(guān)于mybatis遇到Integer類型的參數(shù)時(shí)動(dòng)態(tài)sql需要注意條件
這篇文章主要介紹了關(guān)于mybatis遇到Integer類型的參數(shù)時(shí)動(dòng)態(tài)sql需要注意條件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
IntelliJ IDEA 使用經(jīng)驗(yàn)總結(jié)(推薦)
這篇文章主要介紹了IntelliJ IDEA 使用經(jīng)驗(yàn)總結(jié),非常不錯(cuò),具有參考價(jià)值,需要的朋友可以參考下2018-02-02
Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼
在使用Mybatis時(shí),有的時(shí)候我們可以不用定義resultMap,而是直接在<select>語(yǔ)句上指定resultType。這個(gè)時(shí)候其實(shí)就用到了Mybatis的結(jié)果集自動(dòng)映射,下面通過本文給大家分享Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼,一起看看吧2017-02-02
SpringBoot排除不需要的自動(dòng)配置類DataSourceAutoConfiguration問題
這篇文章主要介紹了SpringBoot排除不需要的自動(dòng)配置類DataSourceAutoConfiguration問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
MyBatis輸入映射和輸出映射的實(shí)現(xiàn)示例
本文詳細(xì)介紹了MyBatis框架中resultMap的使用,包括其在Java對(duì)象與數(shù)據(jù)庫(kù)表列映射中的作用,以及如何配置resultMap處理不同情況,具有一定的參考價(jià)值,感興趣的可以了解一下2026-04-04

