业务描述:由于处理数据量庞大,多表进行拆分,每次任务新建一个表。在往新建表(表名要传参)插入数据时,提示错误:
Error updating database. Cause: java.sql.SQLSyntaxErrorException: 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 ''sms_send_108'(`id`, `task_id`, `mob`, `send_time`, `is_ok`, `sms_id`, `up_msg`' at line 1
### The error may exist in file [XXX-system\taskMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into ?(`id`, `task_id`,up_msg`, `up_ok`,`dept_id`。。。) values (?,?, ?, ?, ?。。。)
### Cause: java.sql.SQLSyntaxErrorException: 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 ''s_send_108'(`id`, `task_id`, `mob`, `send_time`, 。。。' at line 1
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 ''s_send_108'(`id`, `task_id`, `mob`, `send_time`, `。。。' at line 1
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
先自己看,提示:看表名。看出来问题了么?
红圈所示:
解决方法:mybatis 使用#{}传参时,会给参数添加 单引号 变成字符串。使用 ${} 时不会添加单引号。#{}可以进行SQL的预编译,但是 ${}是不行的
然后就能自由的建表和写入了:
问题:表名添加了引号,导致找不到表。
解决办法:Mybatis mapper里面修改:表名
insert into ${tableName}