Mybatis insert error cause: Java sql. SQLException: SQL String cannot be empty
1. Error description
Scenario reproduction: when using mybatis to import a list for batch insertion, the code is as follows:
mapper
void insertTest(List<Test> list);
mapper.xml
<insert id="insertTest" parameterType="java.util.List">
<if test="list != null and list.size() > 0">
INSERT INTO test (test1, test2)
VALUES
<foreach collection="list" index="index" item="item" separator=",">
(#{item.test1}, #{item.test2})
</foreach>
</if>
</insert>
The reason for the error is that the list is passed in The list with size () 0 causes the SQL statement to be empty and an error is reported
Solution:
1. Make non empty judgment (list! = null &&! List. Isempty()) before using mapper, and set mapper If statement removal in XML
2. Use the choose, when and otherwise tags to judge. If it is empty, give a statement to query the empty string
Specific examples are as follows
<insert id="insertTest" parameterType="java.util.List">
<choose>
<when test="list != null and list.size() > 0">
INSERT INTO test (test1, test2)
VALUES
<foreach collection="list" index="index" item="item" separator=",">
(#{item.test1}, #{item.test2})
</foreach>
</when>
<otherwise>
select ""
</otherwise>
</choose>
</insert>
If the scenario needs to implement the insert statement multiple times, it will not be elegant to judge the space multiple times in the code. You can consider using the following solutions for reference only. If there are better methods, you can exchange and discuss them
Read More:
- [Solved] mybatis Error querying database. Cause: java.sql.SQLException: The server time zone value
- [Solved] java.sql.SQLException: Unsupported character encoding ‘utf-8
- [Solved] Error updating database. Cause: java.sql.SQLException: Incorrect integer value: ‘**‘ for column
- Java.sql.sqlexception: unable to read more data from socket
- [Solved] java.sql.SQLException: Unknown system variable cache query size
- [Solved] Error updating database. Cause: java.sql.SQLException: Unknown initial character set index ‘255‘ re
- [Solved] java.sql.SQLException: Table ‘xxx.hibernate_sequence’ doesn’t exist
- Error querying database.Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource.
- [How to Fix]java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x98\x82\xF0\x9F…’
- API Failed to Connect phoenix Error: java.sql.SQLException: ERROR 726 (43M10): Inconsistent namespace mapping properties
- [Solved] JAVA Operate Database Error: You have an error in your SQL syntax; Dao layer SQL statement error
- [Solved] IDEA jdbc Connect Database Error: java.sql.SQLException: Undefined Error
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- Mybatis-plus: How to Execute Native SQL
- How to Solve SQL comments error in the mybatis query
- Mybatis plus configuration console prints complete SQL statement with parameters
- [Solved] mybatis plus Insert Error: mybatis plus Error setting null for parameter #1 with JdbcType OTHER
- [Solved] Mybatis add dependencies Error: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
- JAVA Error: Error attempting to get column ‘XXX’ from result set. Cause java.sql.
- [Solved] Could not find resource COM / atguigu / Dao / studentdao.xm, the mapper file for storing SQL statements could not be found and an error occurred