1. 程式人生 > >mybatis foreach in查詢語句 關於逗號 空值處理問題

mybatis foreach in查詢語句 關於逗號 空值處理問題

原查詢程式碼:

Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;

。。。。。。。。
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘group by mach_id,code
) d1,
(
select mach_id,sum(price) as total_price, m1’ at line 27
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

最終處理:
加上 machList的長度判斷即可
如果machIdList 以逗號輸入的話
machGoodsDaySale.getMachIdList().split(“,”)
得到的陣列長度將是為0 ,多個逗號連續的也是一樣的

        <!-- 多個專櫃 -->
        <if test="machList != null and machList != '' and machList.length > 0">
            AND m1.code in 
            <foreach collection="machList" index="index" item="item" open="(" separator="," close=")"> 
                    #{item} 
            </foreach> 
        </if>