Java中使用Mybatis批量插入数据时Mapper.xml中的sql如下:
<insert id="batchSave">
into t_emp(emp_name,emp_email,dept_id) VALUES <foreach collection="list" item="emp" separator=","> (#{emp.empName}, #{emp.empEmail}, #{emp.deptId}) </foreach></insert>DAO层方法:/**
* * @Title: batchSave * @Description: 批量保存 * @param List * @throws Exception */public void batchSave(List<Emp> empList);修改MyBatis测试类,增加测试方法testBatchSave@Test
public void testBatchSave() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmpMapper mapper = openSession.getMapper(EmpMapper.class); List<Emp> empList = new ArrayList<Emp>(); empList.add(new Emp(null,"Lucy","Lucy@sina.com",1)); empList.add(new Emp(null,"Lily","Lily@sina.com",2)); empList.add(new Emp(null,"Lizy","Lizy@sina.com",3)); //一定要先检查empList不为空再插入 if(empList!=null){ mapper.batchSave(empList); } //别忘了事务最后要提交 openSession.commit(); } finally { openSession.close(); }}出现错误如下: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 1......
解决办法:1.检查SQL语句是不是出现SQL固定的关键字(不论大小写,都不能出现);
2.不确定的话将表名,字段名用`[ESC下面的按钮]引用起来;
3.检查Java代码中批量的插入的List是不是为空,一定要判断不为空再调用批量插入函数;
4.连接mysql的url后面加上&allowMultiQueries=true;
5.使用mybtis的Batch类型的Executor使用是否正确;
SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
下面列出MySQL所有关键字,希望给使用MySQL的朋友提供一些参考帮助。
ADD ALL ALTER
ANALYZE AND ASASC ASENSITIVE BEFOREBETWEEN BIGINT BINARYBLOB BOTH BYCALL CASCADE CASECHANGE CHAR CHARACTERCHECK COLLATE COLUMNCONDITION CONNECTION CONSTRAINTCONTINUE CONVERT CREATECROSS CURRENT_DATE CURRENT_TIMECURRENT_TIMESTAMP CURRENT_USER CURSORDATABASE DATABASES DAY_HOURDAY_MICROSECOND DAY_MINUTE DAY_SECONDDEC DECIMAL DECLAREDEFAULT DELAYED DELETEDESC DESCRIBE DETERMINISTICDISTINCT DISTINCTROW DIVDOUBLE DROP DUALEACH ELSE ELSEIFENCLOSED ESCAPED EXISTSEXIT EXPLAIN FALSEFETCH FLOAT FLOAT4FLOAT8 FOR FORCEFOREIGN FROM FULLTEXTGOTO GRANT GROUPHAVING HIGH_PRIORITY HOUR_MICROSECONDHOUR_MINUTE HOUR_SECOND IFIGNORE IN INDEXINFILE INNER INOUTINSENSITIVE INSERT INTINT1 INT2 INT3INT4 INT8 INTEGERINTERVAL INTO ISITERATE JOIN KEYKEYS KILL LABELLEADING LEAVE LEFTLIKE LIMIT LINEARLINES LOAD LOCALTIMELOCALTIMESTAMP LOCK LONGLONGBLOB LONGTEXT LOOPLOW_PRIORITY MATCH MEDIUMBLOBMEDIUMINT MEDIUMTEXT MIDDLEINTMINUTE_MICROSECOND MINUTE_SECOND MODMODIFIES NATURAL NOTNO_WRITE_TO_BINLOG NULL NUMERICON OPTIMIZE OPTIONOPTIONALLY OR ORDEROUT OUTER OUTFILEPRECISION PRIMARY PROCEDUREPURGE RAID0 RANGEREAD READS REALREFERENCES REGEXP RELEASERENAME REPEAT REPLACEREQUIRE RESTRICT RETURNREVOKE RIGHT RLIKESCHEMA SCHEMAS SECOND_MICROSECONDSELECT SENSITIVE SEPARATORSET SHOW SMALLINTSPATIAL SPECIFIC SQLSQLEXCEPTION SQLSTATE SQLWARNINGSQL_BIG_RESULT SQL_CALC_FOUND_ROWS SQL_SMALL_RESULTSSL STARTING STRAIGHT_JOINTABLE TERMINATED THENTINYBLOB TINYINT TINYTEXTTO TRAILING TRIGGERTRUE UNDO UNIONUNIQUE UNLOCK UNSIGNEDUPDATE USAGE USEUSING UTC_DATE UTC_TIMEUTC_TIMESTAMP VALUES VARBINARYVARCHAR VARCHARACTER VARYINGWHEN WHERE WHILEWITH WRITE X509XOR YEAR_MONTH ZEROFILL