问题

Mapper在执行查询语句时,能查询出正确结果

@Select("select * from seller_categories where seller_id=#{seller_id}")
List<Seller_categories> getSellCategory(Integer seller_id);

image-20240423234356004

然而map到返回值时,却全是null

aad7b7b4c31b8255d91ca3e5b894999

其中,Seller_categories类定义如下:

public class Seller_categories {
private Integer seller_id;
private Integer category_id;
private Integer sub_category_id;
private Integer is_all_sub;
}

驼峰转换已开启

map-underscore-to-camel-case: true

过程

看了最后付的那篇文章,我突然灵光一闪,将Seller_categories类小改一下:

public class Seller_categories {
private Integer sellerId;
private Integer category_id;
private Integer sub_category_id;
private Integer is_all_sub;
}

您猜怎么着,还真可以

image-20240423234908194

我的母语是无语。。。

所以猜测:驼峰转换转的是数据库的,而类中的不会转,所以类如果用的是下划线,查询时数据库的转成了驼峰,此时就map不上

解决方法

定义类时使用驼峰命名即可。

顺便,如果需要使用类去接收Json的传参(特指boolean类型的),需要使用@JsonProperty进行注解。

@JsonProperty("isDefault")
private boolean isDefault;

因为这个参数方法也是isDefault()RPC框架在反向解析的时候,会误判deleted才是属性名称,导致属性获取不到,进而产生抛出异常或者传参失败等情况。

参考链接

Mapper返回为NULL的问题(Springboot2.0.4+Mybatis3.4.6+MySQL)_查找usermapper可以是null的原因-CSDN博客

一文让你明白@RequestBody接收不到boolean值的原因_前端传boolean传入不进去-CSDN博客