SoFunction
Updated on 2025-03-08

Mybatis report Query was Empty exception problem

Mybatis reported Query was Empty exception

The <foreach> tag in mybatis is often used during batch processing, and what is inevitable with <foreach> is to make non-empty judgments on the list, otherwise it is easy to make errors.

Today when I was inserting in batches, my code looks like this

<if test="list!=null and >0>
   insert into tables
   values
   <foreach item="item" separator="," open="(" close=")">
#{item}
</foreach>
</if>

I thought it would be enough to add a judgment outside, but this exception was thrown during runtime: Query was Empty. So how did this exception come about? In fact, when List is empty, the <if> tag is judged, so there is no content in the <insert> tag, which leads to this exception: Query was Empty.

I always think this thing should be a flaw of Mybatis. Why do I have to judge this list every time? And I feel that it is still very troublesome to judge. I hope that the foreach tag will be better supported in the future and will be more enjoyable to use.

Mybatis batch update appears Query was Empty

When I checked that other situations were correct, the background still reported me the error "query was empty", which means "query is empty". The expression here is inaccurate. This is because your incoming list collection is an empty set, not an exception caused by the query result being empty. Therefore, we need to judge whether the set is passed in a set with parameters, so as not to let mybatis directly throw a large number of unfriendly exceptions. Let's record it here.

    <update  parameterType="">
        <foreach collection="list" item="items" separator =";">
            update item set
            enroll1=#{items.enroll1},
            enroll1=#{items.enroll2},
            business1=#{items.business1},
            business2=#{items.business2}
            where id=#{}
        </foreach>
    </update>

The above is personal experience. I hope you can give you a reference and I hope you can support me more.