mybatis返回map类型数据空值字段不显示(解决方法)

版权声明:本人原创,转载需说明文章出处     https://blog.csdn.net/persistencegoing/article/details/89704545

https://blog.csdn.net/persistencegoing/article/details/84376427

如题,当使用spring boot加mybatis时,设置Map返回,当值为空时属性也会没有

在application.properties中加入下面配置,将会解决这个问题。

#当查询数据为空时字段返回为null,不加这个查询数据为空时,字段将被隐藏
mybatis.configuration.call-setters-on-nulls=true
其它解决办法

1.查询sql添加每个字段的判断空

IFNULL(rate,'') as rate
2.ResultType利用实体返回,不用map(这其实算不上一种解决办法)

3.springMVC+mybatis查询数据,返回resultType=”map”时,如果数据为空的字段,则该字段省略不显示,可以通过添加配置文件,规定查询数据为空是则返回null。

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

希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职     

群号:721 515 304

猜你喜欢

转载自blog.csdn.net/persistencegoing/article/details/89704545