MySQL5.7 + Mybatis 下面语句,写了两条delete语句,并用分号分割,
<delete id="delArticleComments" parameterType="java.lang.String">
<!--#根据文章id删除评论和回复
#1、根据评论id删除所有回复
#先删除所有回复,,,Mysql读写锁锁定的问题,需创建临时表 -->
DELETE FROM lx_reply WHERE
id IN (
<!--#建立临时表 -->
SELECT id FROM(
<!--#查出符合的回复id集合-->
SELECT id FROM lx_reply WHERE comment_id IN
(
<!--#查出所有评论id-->
SELECT id FROM lx_comment WHERE topic_id = #{articleId}
)
)AS tmp
);
<!-- #2、删除所有评论 -->
DELETE FROM lx_comment WHERE topic_id = #{articleId, jdbcType=VARCHAR};
</delete>
mybatis一个标签能写多条语句,可是报错:
### Error querying 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 'DELETE FROM lx_comment WHERE topic_id = 'EtTkC8Nr'' at line 14
### The error may exist in file [F:\ideaworkspace\LeixingBlog\target\classes\mapper\LxArticleMapper.xml]
### The error may involve com.leixing.blog.mapper.LxArticleMapper.delArticleComments-Inline
### The error occurred while setting parameters
### SQL: DELETE FROM lx_reply WHERE id IN ( SELECT id FROM( SELECT id FROM lx_reply WHERE comment_id IN ( SELECT id FROM lx_comment WHERE topic_id = ? ) )AS tmp ); DELETE FROM lx_comment WHERE topic_id = ?;
### 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 'DELETE FROM lx_comment WHERE topic_id = 'EtTkC8Nr'' at line 14
可有确定语句没有错,,,最后
看到 https://www.cnblogs.com/jeffen/p/6038261.html
MySQL+ MyBatis 中一次执行多条语句,需要在连接上加 &allowMultiQueries=true
但是我在之前写批量删除下面语句的时候就没加,也没出错,,,想想下面的算一次操作,也就是一条语句,通了
<!-- 批量删除 -->
<delete id="deleteByIds" parameterType="java.lang.String" >
DELETE FROM lx_article WHERE article_id IN
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
解决如下:
#Mybatis+MySql 一个标签中执行自己写的多条sql语句报错 &allowMultiQueries=true
spring.datasource.url=jdbc:mysql://www.leixing.xyz:3306/leixing?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true