困扰了三天的问题,报错内容如下
2020-08-10 17:31:57.501 INFO 3040 --- [nio-8880-exec-2] c.h.r.a.controller.QuestionController : insertQuestion: params data ===> [{"answer":"你是猪吗","degree":0,"domain":0,"image":"你是猪吗","question":"你是猪吗"}]
2020-08-10 17:31:57.609 INFO 3040 --- [nio-8880-exec-2] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2020-08-10 17:31:57.673 ERROR 3040 --- [nio-8880-exec-2] com.hi.rongyao.base.BaseController : 请求路径:/question/insert 请求方式:POST url拼接参数:null
2020-08-10 17:31:57.677 WARN 3040 --- [nio-8880-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [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 'option, image, answer, degree, domain
)
values
' at line 2
### The error may involve com.hi.rongyao.affair.mapper.QuestionMapper.insertQuestion-Inline
### The error occurred while setting parameters
### SQL: insert into question ( question, option, image, answer, degree, domain ) 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 'option, image, answer, degree, domain
)
values
' 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 'option, image, answer, degree, domain
)
values
' at line 2]
1、mybatis
select last_insert_id() as id
insert into (
question, option, image, answer, degree, domain
)
values
(
#{item.question}, #{item.option}, #{item.image}, #{item.answer}, #{item.degree}, #{item.domain}
)
2、实体
public class Question {
@Getter @Setter private Integer id;
@Getter @Setter private String question; // 问题
@Getter @Setter private String option; // 选项列表
@Getter @Setter private String answer; // 答案
@Getter @Setter private Integer degree; // 难度 0-低 1-中 2-高
@Getter @Setter private Integer domain; // 范畴
@Getter @Setter private String image; // 图片
@Getter @Setter private Date createTime; // 创建时间
@Getter @Setter private Date updateTime; // 更新时间
}
3、mapper
// 批量插入
int insertQuestion(List list);
4、controller
// 批量插入
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public @ResponseBody ReturnObject insert(@RequestBody List list) {
logger.info("insertQuestion: params data ===> " + JSON.toJSONString(list));
questionService.insertQuestion(list);
return new ReturnObject(ReturnCode.SUCCESS);
}
由于是小白,一开始怎么也找不到问题原因所在,今天终于弄明白,原来是使用了mysql的关键字 option 导致的
天杀的,足足困扰了我三天,百思不得其解,小白踩的坑要填一下了
传送门,msql 保留关键字
https://www.cnblogs.com/wuyifu/p/5949764.html