list.size() = 1 in Mybatis but it shows All elements are null

1. Bug display

2. Reason analysis

2.1. Situation 1: Return type mapping error in XML of Mybatis


<select id="selectByDesc" parameterType="com.task.bean.OrderInfo"
            resultType="com.task.bean.OrderInfo">
        select MER_ID,SETTLE_DATE,ICE_NAME,ORDER_STATUS,ORDER_CODE,ORDER_DESC,
        COUNT(*) AS SUM_COUNT,SUM(ORDER_AMT) AS SUM_AMT
        FROM order_info_${ny}
        <where>
            <if test="settleDate != null and settleDate != ''">
                AND SETTLE_DATE = #{settleDate}
            </if>
        </where>
        GROUP BY SETTLE_DATE
</select>

 At this point, the resultType corresponds to the entity bean, but the queried data cannot be returned normally due to the incomplete correspondence of the parameters or the incomplete match of the parameter types. At this point, you can adjust the entity bean or modify the xml to make the fields completely correspond; or use resultMap to redefine the return parameters , as shown below:

<resultMap id="orderInfo" type="com.task.bean.OrderInfo">
        <result property="orderId" column="ORDER_ID"/>
        <result property="merId" column="MER_ID"/>
        <result property="merName" column="MER_NAME"/>
 
        *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
 
        <result property="sumCount" column="SUM_COUNT"/>
</resultMap>
 
 
 
 
<select id="selectByDesc" parameterType="com.task.bean.OrderInfo"
            resultMap="orderInfo">    
        select MER_ID,SETTLE_DATE,ICE_NAME,ORDER_STATUS,ORDER_CODE,ORDER_DESC,
        COUNT(*) AS SUM_COUNT,SUM(ORDER_AMT) AS SUM_AMT
        FROM order_info_${ny}
        <where>
            <if test="settleDate != null and settleDate != ''">
                AND SETTLE_DATE = #{settleDate}
            </if>
        </where>
        GROUP BY SETTLE_DATE
</select>

2.2. Scenario 2: The aggregate function in MySQL is used

The aggregation functions in MySQL are the summation function SUM(), the average function AVG(), the maximum value function MAX(), the minimum value function MIN(), and the counting function COUNT, resulting in size = 1, but The case where the result is empty.

At this time, no matter whether there is a value in the database or not , the return is not empty. Through the above debugging steps, it is found that  list.size() = 1, but the value in the List is empty, showingAll elements are null

At this time, if the xml cannot be optimized , the business layer code can be adjusted, for example:

if (CollectionUtils.isNotEmpty(listData) && listData.get(0) != null) {
    
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
 
}

Manually eliminate this situation. Or, first exclude the null elements in the list, and then process the business data

//移除第一个null
 
list.remove(null); 
 
//或移除所有的null元素
 
list.removeAll(Collections.singleton(null)); 

3. Aggregation function 

When Java uses aggregate functions to query, the reason why the returned result must not be null is that the definition of aggregate functions determines their behavior. Aggregate functions are used to perform calculations on a set of values ​​and return a result. By definition, aggregate functions always return a non-null result, even if the input is empty or invalid. This is to ensure consistency and reliability of calculations.

        For example, if you use the SUM() aggregate function to calculate the sum of a set of values, it will return 0 as the result even if the input is empty. Likewise, if you use the AVG() aggregate function to calculate the average of a set of values, it will return 0 as the result even if the input is empty.

        Therefore, when Java uses an aggregate function to query, it can ensure that the returned result must not be null, but return an appropriate value according to the definition of the aggregate function. Similarly, this also causes data to need to manually check out NULL under certain circumstances.
 

Reposted from: list.size() = 1 in Mybatis but it shows All elements are null_lollipop sugar sugar-free blog-CSDN blog

Guess you like

Origin blog.csdn.net/gaoshan12345678910/article/details/132585571