在MyBatis中,查詢結果resultMap返回的是實體類,實體類中則處理值爲null的結果。java
如:數據庫
public void setNickname(String nickname) { this.nickname = nickname == null ? null : nickname.trim(); }
可是當返回結果爲自定義resultMap時,就會出現,如下狀況:mybatis
如:測試
自定義mapui
<resultMap type="java.util.Map" id="userLoginInfo" > <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>
返回結果:this
{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時都不存在code
解決方法:xml
一、在Mybatis_config.xml文件中添加配置信息token
<?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>
經測試,值爲null的結果在map中存在io
測試結果:
{accountLocked=false, loginTime=1480559610977, userHead=null, phone=18301413850, isDelete=false, dataProgress=0, nickname=, id=93cd68fcb95043febd28d6ad86666029, uuid=544dc45953e2dcc60e23ecccc52c578b, token=WT1Kgx}