在日常开发中,查询数据返回类型为map,数据库中有些自动值为空,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢?
1.xml文件:
<resultMap id="userLoginInfo" type="map" >
<result column="uuid" property="id" />
<result column="uuid2" property="uuid" />
<result column="account" property="phone" />
<result column="login_time" property="loginTime" />
<result column="token" property="token" />
<result column="data_progress" property="dataProgress" />
<result column="account_locked" property="accountLocked" />
<result column="is_delete" property="isDelete" />
<result column="nickname" property="nickname" />
<result column="user_head" property="userHead" />
</resultMap>
返回结果:
{accountLocked=false, loginTime=1480559610977, phone=18301413850, isDelete=false, dataProgress=0, nickname=,
id=93cd68fcb95043febd28d6ad86666029, uuid=544dc45953e2dcc60e23ecccc52c578b, token=WT1Kgx}
发现返回结果Map中没有user_head字段,原来 user_head在数据库中值为null,所以说数据库中所有值为null的字段在查询接收resultMap时都不存在
解决方法:
1、在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>
<!--解决,查询返回结果含null没有对应字段值问题-->
<setting name="callSettersOnNulls" value="true"/>
</settings>
</configuration>
备注:注意xml文件的头部引入的是mybaties-3-config.dtd
测试结果:
{accountLocked=false, loginTime=1480559610977, userHead=null, phone=18301413850, isDelete=false, dataProgress=0, nickname=,
id=93cd68fcb95043febd28d6ad86666029, uuid=544dc45953e2dcc60e23ecccc52c578b, token=WT1Kgx}
经测试,值为null的结果在map中存在