【SpringBoot】使用mybatis查询返回map时空值不保存key的解决方法

在使用mybatis对数据库进行查询然后返回一个map类型的时候,如果某一列的值为空

那么返回的map里是会没有这个值对应的列的key,也就是map的容量会比预期小,很容易出一些问题

我在前端对传来的map遍历存入table时,由于缺少一些key,导致后面的列往前面推,使表格混乱

为了解决这个问题,我先是找到了这篇博客,分析了一下mybatis给map类型赋值的过程

https://blog.csdn.net/zdazx/article/details/79289074


但是它使用的是ssm框架,为了在springboot框架里配置mybatis配置,我新建了一个mybatis-config.xml配置文件,然后再springboot配置文件application.properties里引入

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
<configuration>  
 <settings> <setting name="callSettersOnNulls" value="true"/>  
 <setting name="returnInstanceForEmptyRow" value="true" />  
 </settings></configuration>

接着配置

mybatis-plus.mapper-locations=classpath*:/mapper/*.xml  
mybatis-plus.config-location=classpath:/mybatis-setting.xml

这里由于我使用的是mybatis-plus,前面需要更改为mybatis-plus

另外,直接配置mybatis.configuration.returnInstanceForEmptyRow.value=true这种是不行的,没有效果