Springboot編寫CRUD時訪問對應(yīng)數(shù)據(jù)函數(shù)返回null的問題及解決方法
1. 我遇到了什么問題
我在學(xué)習(xí)springboot,其中在編寫CRUD時發(fā)現(xiàn)訪問數(shù)據(jù)的函數(shù)執(zhí)行下去返回值是null但是其它部分正常。
下面是我的錯誤代碼
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ù)庫列名

其中注意我是在面臨訪問user_id這個類時出現(xiàn)了返回null。當(dāng)時目的是為了pojo和數(shù)據(jù)庫對應(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ù)庫的函數(shù)都是idea自動填充的
問題就是當(dāng)我程序進行到這個頁面時,bot.getUser_id()返回值是null其它值都是正確的
2. 我是怎么做得
后面發(fā)現(xiàn)pojo層的命名和數(shù)據(jù)庫之間要使用駝峰命名法進行對應(yīng),關(guān)于駝峰命名法希望大家自己去查一查,因為我也不熟。但是對于數(shù)據(jù)庫中的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 ;
}同時把service中的
bot.getUser_id()
改為
bot.getUserId()
問題就解決了
到此這篇關(guān)于Springboot在編寫CRUD時,訪問對應(yīng)數(shù)據(jù)函數(shù)返回null的文章就介紹到這了,更多相關(guān)Springboot編寫CRUD返回null內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Shiro框架,實現(xiàn)用戶權(quán)限管理
Apache Shiro是一個強大且易用的Java安全框架,執(zhí)行身份驗證、授權(quán)、密碼和會話管理。作為一款安全框架Shiro的設(shè)計相當(dāng)巧妙。Shiro的應(yīng)用不依賴任何容器,它不僅可以在JavaEE下使用,還可以應(yīng)用在JavaSE環(huán)境中。2021-06-06
關(guān)于mybatis遇到Integer類型的參數(shù)時動態(tài)sql需要注意條件
這篇文章主要介紹了關(guān)于mybatis遇到Integer類型的參數(shù)時動態(tài)sql需要注意條件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
IntelliJ IDEA 使用經(jīng)驗總結(jié)(推薦)
這篇文章主要介紹了IntelliJ IDEA 使用經(jīng)驗總結(jié),非常不錯,具有參考價值,需要的朋友可以參考下2018-02-02
SpringBoot排除不需要的自動配置類DataSourceAutoConfiguration問題
這篇文章主要介紹了SpringBoot排除不需要的自動配置類DataSourceAutoConfiguration問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07

