鱼C论坛

 找回密码
 立即注册
查看: 131|回复: 3

[已解决]响应数据里字段值全为null是哪里出问题了

[复制链接]
发表于 2024-3-12 21:54:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
下面是一段响应数据
{
    "code": 0,
    "message": "操作成功",
    "data": [
        {
            "id": 1,
            "categoryName": null,
            "categoryAlias": null,
            "createUser": null,
            "createTime": null,
            "updateTime": null
        },
        {
            "id": 2,
            "categoryName": null,
            "categoryAlias": null,
            "createUser": null,
            "createTime": null,
            "updateTime": null
        }
    ]
}
在数据库里 字段值全不为null 但是 响应数据里字段值全为null是哪里出问题了

CategoryController类里代码如下
package com.example.demo.controller;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.pojo.Category;
import com.example.demo.pojo.Result;
import com.example.demo.service.CategoryService;

@Mapper
@RestController
@RequestMapping("/category")
public class CategoryController {

        @Autowired
        private CategoryService categoryService;
       
        @PostMapping
        public Result add(@RequestBody @Validated Category category) {
                categoryService.add(category);
                return Result.success();
        }
       
        @GetMapping
        public Result<List<Category>> list(){
                List<Category> cs = categoryService.list();
                return Result.success(cs);
               
        }
}

CategoryServiceImpl类里代码如下

package com.example.demo.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.mapper.CategoryMapper;
import com.example.demo.pojo.Category;
import com.example.demo.service.CategoryService;
import com.example.demo.util.ThreadLocalUtil;

@Service
public class CateServiceImpl implements CategoryService{
       
        @Autowired
        private CategoryMapper categoryMapper;

        @Override
        public void add(Category category) {
                Map<String, Object> map = ThreadLocalUtil.get();
                Integer userId = (Integer)map.get("id");
                category.setCreateUser(userId);
                categoryMapper.add(category);
        }

        @Override
        public List<Category> list() {
                Map<String, Object> map = ThreadLocalUtil.get();
                Integer userId = (Integer)map.get("id");
                return categoryMapper.list(userId);
               
        }

}

CategoryMapper类里代码如下

package com.example.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.example.demo.pojo.Category;

@Mapper
public interface CategoryMapper {

        @Insert("insert into category (category_name,category_alias,create_user,create_time,update_time)"+
        "value(#{categoryName},#{categoryAlias},#{createUser},now(),now())")
        void add(Category category);

        @Select("select * from category where create_user=#{userId}")
        List<Category> list(Integer userId);

}


请问出现响应数据里字段值全为null的原因是什么
最佳答案
2024-3-12 21:55:03
出现响应数据里字段值全为null的原因可能是以下几个方面:

1. 数据库中的数据没有正确插入:检查一下数据库插入操作是否正常,可以在add方法中打印一些日志或者调试信息,确认数据是否正确插入到数据库中。

2. 数据库查询操作有误:检查list方法中查询操作是否正确,可以在list方法中打印一些日志或者调试信息,确认查询语句是否正确执行,并返回了期望的结果。

3. 数据库连接配置有误:如果数据库连接配置有问题,例如连接URL、用户名、密码等不正确,可能导致无法正确地读写数据库中的数据。

综合以上几点,可以逐步排查问题所在。如果可以提供更多的错误日志或相关代码,我可以给出更具体的解答。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-12 21:55:03 | 显示全部楼层    本楼为最佳答案   
出现响应数据里字段值全为null的原因可能是以下几个方面:

1. 数据库中的数据没有正确插入:检查一下数据库插入操作是否正常,可以在add方法中打印一些日志或者调试信息,确认数据是否正确插入到数据库中。

2. 数据库查询操作有误:检查list方法中查询操作是否正确,可以在list方法中打印一些日志或者调试信息,确认查询语句是否正确执行,并返回了期望的结果。

3. 数据库连接配置有误:如果数据库连接配置有问题,例如连接URL、用户名、密码等不正确,可能导致无法正确地读写数据库中的数据。

综合以上几点,可以逐步排查问题所在。如果可以提供更多的错误日志或相关代码,我可以给出更具体的解答。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-13 01:09:28 | 显示全部楼层
数据库中的数据已经正确插入 数据库查询操作无误 数据库连接配置无误 但是问题依旧存在
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-13 01:09:38 | 显示全部楼层
mumei2018 发表于 2024-03-13 01:09
数据库中的数据已经正确插入 数据库查询操作无误 数据 ...


从您提供的代码中观察到一个潜在的问题是,如果在`ThreadLocalUtil.get()`方法中获取到的`map`为空,那么`userId`将会是null。这可能会导致`categoryMapper.list(userId)`方法返回空结果。

您可以在`CategoryServiceImpl`中的`list`方法中添加一些日志或调试信息来验证`userId`是否为null,例如:

  1. @Override
  2. public List<Category> list() {
  3.     Map<String, Object> map = ThreadLocalUtil.get();
  4.     Integer userId = (Integer) map.get("id");
  5.     System.out.println("userId: " + userId); // 打印userId验证是否为null
  6.     return categoryMapper.list(userId);
  7. }
复制代码


如果`userId`确实为null,您需要检查`ThreadLocalUtil`类中的实现,确保正确地设置了`map`值。

另外,建议您确认`Category`类定义的成员变量和数据库表的字段名是否一致。如果存在不一致的情况,可能会导致响应数据中的字段值为null。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 06:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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