简体   繁体   中英

How to return columns with null values in a result map in mybatis

<select id="keyEquipmentShiftAutomatedModelData" parameterType="java.util.Map" resultType="java.util.Map">
SELECT  ID, SUM(VALUE1 + VALUE2) Total
FROM    tableName
GROUP   BY ID
    </select>

Returns below result in sql

ID, Total
1   (null)

But when returned in Map result in myBatis returns {ID=1} where as Total is missing. How to get the column with null into the result set as well something like this {ID=1,Total=null} or something like this {ID=1,Total=""}

Adding

<settings>
<setting name="callSettersOnNulls" value="true"/>
</settings>

mybatis-config.xml fixes the issue if using mybatis 3.2 + version

谢谢,它有效,除了您需要转换价值

columns[i] = (value == null) ? "" : String.valueOf(value);

使用普通Sql中的coalesce可以轻松解决此问题:

COALESCE( SUM(VALUE1 + VALUE2), '') as Total

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM