使用mybatis的批量insert功能,但当List的size为0时,报sql语法错误!
业务逻辑代码如下,通过接口获取json数据,解析成Shooter对象,放到shooterList中,然后调用批量insert的方法。
List<Shooter> shooterList = new ArrayList<Shooter>();
//中超
Shooters shooters_zhongchao = ParseJSONUtility.parseShooterJSON(PathUtility.SHOOTERURL,configInfo.seasonid_zhongchao,PathUtility.APIKEY);
for (Shooter shooter : shooters_zhongchao.getShooters()){
shooter.setSeason(configInfo.seasonid_zhongchao);
shooterList.add(shooter);
}
shooterService.addShooterBatch(shooterList);
mybatis配置文件的写法如下:
<insert id="addShooterBatch" parameterType="java.util.List">
INSERT INTO shooter(jq,scsj,zg,playerid,name,teamid,teamname,rank,season)
VALUES
<foreach collection="list" item="item" index="index" open="" close="" separator=",">
(#{item.jq},#{item.scsj},#{item.zg},#{item.playerid},#{item.name},#{item.teamid},#{item.teamname},#{item.rank},#{item.season})
</foreach>
</insert>
报错:
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: 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 '' at line 2
### The error may involve com.cheerland.wechat.dao.ShooterMapper.addShooterBatch-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO shooter(jq,scsj,zg,playerid,name,teamid,teamname,rank,season) VALUES
### Cause: 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 '' at line 2
; bad SQL grammar []; nested exception is 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 '' at line 2