ToB企服应用市场:ToB评测及商务社交产业平台

标题: Springboot在编写CRUD时,访问对应数据函数返回null [打印本页]

作者: 丝    时间: 2024-4-25 04:56
标题: Springboot在编写CRUD时,访问对应数据函数返回null
1. 我遇到了什么问题

我在学习springboot,其中在编写CRUD时发现访问数据的函数执行下去返回值是null但是其它部分正常。
下面是我的错误代码
pojo
  1. public class Bot {
  2.     @TableId(type = IdType.AUTO )
  3.     private Integer id ;
  4.     private  Integer user_id ;
  5.     private String name ;
  6.     private String description ;
  7.     private String content ;
  8.     private Integer rating ;
  9.     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  10.     private Date create_time ;
  11.     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  12.     private Date modify_time ;
  13. }
复制代码
数据库列名


其中注意我是在面临访问user_id这个类时出现了返回null。当时目的是为了pojo和数据库对应。
Service
  1. @Service
  2. public class RemoveServiceImpl implements RemoveService {
  3.     @Autowired
  4.     BotMapper botMapper ;
  5.     @Override
  6.     public Map<String, String> remove(Map<String, String> data) {
  7.         UsernamePasswordAuthenticationToken authenticationToken =
  8.                 (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication() ;
  9.         UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal() ;
  10.         User user = loginUser.getUser() ;
  11.         Map<String,String> map = new HashMap<>();
  12.         int bot_id = Integer.parseInt(data.get("bot_id")) ;
  13.         Bot bot = botMapper.selectById(bot_id) ;
  14.         if(bot == null) {
  15.             map.put("error_message", "Bot不存在") ;
  16.             return map ;
  17.         }
  18.         System.out.println("new BOT_ID" + bot.getId());
  19.         System.out.println(bot.getName());
  20.         System.out.println(bot.getUser_id());
  21.         System.out.println(user.getId());
  22.         if(!bot.getUser_id().equals(user.getId())) {
  23.             map.put("error_message", "你没有权限") ;
  24.             return map ;
  25.         }
  26.         botMapper.deleteById(bot_id) ;
  27.         map.put("error_message", "success") ;
  28.         return map ;
  29.     }
  30. }
复制代码
其中各类访问数据库的函数都是idea自动填充的
问题就是当我程序进行到这个页面时,bot.getUser_id()返回值是null其它值都是正确的
后面发现pojo层的命名和数据库之间要使用驼峰命名法进行对应,关于驼峰命名法希望大家自己去查一查,因为我也不熟。但是对于数据库中的user_id列命名需要把_变为大写。
将pojo层变为
  1. public class Bot {
  2.     @TableId(type = IdType.AUTO )
  3.     private Integer id ;
  4.     private Integer userId ;
  5.     private String name ;
  6.     private String description ;
  7.     private String content ;
  8.     private Integer rating ;
  9.     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  10.     private Date create_time ;
  11.     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  12.     private Date modify_time ;
  13. }
复制代码
同时把service中的
  1. bot.getUser_id()
复制代码
改为
  1. bot.getUserId()
复制代码
问题就解决了
我是看这位大佬的提醒懂得

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4