Springboot在编写CRUD时,访问对应数据函数返回null

  金牌会员 | 2024-4-25 04:56:06 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 881|帖子 881|积分 2643

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()
复制代码
问题就解决了
我是看这位大佬的提醒懂得

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表