项目场景:
在使用SpringBoot整合Mybatis时报错
问题描述
查询结果中a_id内容为空
mapper:
@Select("select * from t_comment where id = #{id}")
Comment findById(Integer id);
test:
@Autowired
private CommentMapper cm;
@Test
public void selectComment(){
Comment comment = cm.findById(6);
System.out.println(comment);
}
报错:

原因分析:
在编写实体类时使用了驼峰规则将a_id字段设计成了aId属性,所以无法正确映射查询结果。
解决方案:
在全局配置文件中添加开启驼峰命名匹配映射配置
#开启驼峰命名匹配映射
mybatis.configuration.map-underscore-to-camel-case=true
问题解决:


在SpringBoot集成Mybatis的过程中遇到一个问题,当查询t_comment表中id为指定值的数据时,由于实体类中的a_id字段按照驼峰规则被命名为aId,导致字段映射失败。为了解决这个问题,可以在Mybatis的全局配置中启用驼峰命名到下划线转换的映射,通过设置`mybatis.configuration.map-underscore-to-camel-case=true`,使得数据库中的字段名能正确匹配到实体类的属性,从而成功获取并打印查询结果。
1282

被折叠的 条评论
为什么被折叠?



