作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图使用 mybatis 通过单个查询插入 4 个表,但不幸的是在这样做时出现错误,请帮助我。
映射器.xml
<insert id="insert" parameterType="com.technoshinelabs.eduskill.bean.Course" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
START TRANSACTION;
INSERT INTO course (course_name, training_provider, type_of_course, training_duration, number_of_license, type_of_users, about_course,
who_can_attend, certificate, training_type, course_image,created_date,updated_date)
VALUES (#{courseName}, #{trainingProvider}, #{typeOfCourse}, #{trainingDuation}, #{numberOfLicense}, #{typeOfUsers},
#{aboutCourse}, #{whoCanAttend}, #{certificate}, #{trainingType}, #{courseImagePath}, now(), now());
INSERT INTO assignment_material (course_id, file_name)
VALUES (#{courseId},
<foreach item="Course" collection="assignmentMaterialPath">#{Course}</foreach>);
INSERT INTO trainer_image (course_id, file_name)
VALUES (#{courseId},
<foreach item="Course" collection="trainerImagePath">#{Course}</foreach>);
INSERT INTO trainer_provider_logo (course_id, file_name)
VALUES (#{courseId},
<foreach item="Course" collection="trainingProviderPath">#{Course}</foreach>);
COMMIT;
</insert>
如果我尝试执行此操作,则会出现错误:
There was an unexpected error
(type=Internal Server Error, status=500).
### 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 'INSERT INTO course (course_name, training_provider, type_of_course, training_dur' at line 2
### The error may involve com.technoshinelabs.eduskill.mapper.CourseMapper.insert-Inline
### The error occurred while setting parameters
### SQL:
START TRANSACTION;
INSERT INTO course (course_name, training_provider, type_of_course, training_duration, number_of_license, type_of_users, about_course, who_can_attend, certificate, training_type, course_image,created_date,updated_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now());
INSERT INTO assignment_material (course_id, file_name) VALUES (?, ? ? ? ); INSERT INTO trainer_image (course_id, file_name) VALUES (?, ? ? ? );
INSERT INTO trainer_provider_logo (course_id, file_name) VALUES (?, ? ? ? );
COMMIT;
### 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 'INSERT INTO course (course_name, training_provider, type_of_course, training_dur' 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 'INSERT INTO course (course_name, training_provider, type_of_course, training_dur' at line 2
最佳答案
删除 START TRANSACTION;
和 COMMIT;
它们不能是查询的一部分。
4 个插入将在单个语句中执行。但事务必须由应用程序或容器管理。
交易实际上是在 session 打开时开始的。
关于mybatis - 如何在mybatis中使用单个查询将值插入到多个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43047588/
我是一名优秀的程序员,十分优秀!