本篇文章小编给大家分享一下MyBatis一次执行多条SQL语句操作代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
有个常见的场景:删除用户的时候需要先删除用户的外键关联数据,否则会触发规则报错。
解决办法不外乎有三个:
1、多条sql分批执行;
2、存储过程或函数调用;
3、sql批量执行。
今天要说的是MyBatis中如何一次执行多条语句(使用mysql数据库)。
1、修改数据库连接参数加上allowMultiQueries=true,如:
hikariConfig.security.jdbcUrl=jdbc:mysql://xx.xx.xx:3306/xxxxx?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
2、直接写多条语句,用“;”隔开即可
delete from sec_user_role where userId=#{id}; delete from sec_user where id=#{id};