java执⾏两条update语句,mybatis实现多条update同时执⾏想在mapper的⼀个更新节点进⾏多条update语句的操作:
update user set valid_status = 1 where mobile_phone = #{mobilePhone};
update user_account set valid_status = 1 where mobile_phone = #{mobilePhone} ;批量更新sql语句
mybatis是默认不⽀持的,需要在数据库配置中配置相关参数:
propertes 或者yml配置 ⽂件中的jdbc后追加&allowMultiQueries=true
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
补充:mybatis批量更新update-设置多个字段值
mybatis由于简单易⽤性得到⼤家的认可和使⽤
但是在批量更新操作中,⽹上介绍的貌似不全,正好今天做个记录,⼤家⼀起进步
在实际项⽬开发过程中,常有这样的需求:根据ids更新表的某⼀个字段值,这时的sql语句是:
public interface IStaffDao {
void batchUpdate(@Param("list") List list);
}
update staff set status = 0 where id in
#{item}
ORDER BY id
还有⼀种情况:根据ids更新表的多个值,并且每个id对应的值也不⼀样,这时上述语句已经满⾜不了需求,需要另⼀种批量更新sql语句
public interface IStaffDao {
void batchUpdate(@Param("list") List list);
}
UPDATE staff set count = #{unt} , code = #{de} , invalid_time = #{item.time} WHERE id = #{item.id}
由于这种批量更新是⼀次执⾏多个update语句,所以mybatis需要额外的配置:
在spring.datasource.url后加上allowMultiQueries=true
如:jdbc:mysql://10.10.20.36:3306/test?allowMultiQueries=true
否则,在执⾏sql语句时,会报下⾯的错误
[org.apache.ibatis.session.defaults.DefaultSqlSession@76a2f910]
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: ptions.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 'update b_email_msg_remind
SET send_status = 1, send_email_code='abc@abc.abc'' at line 6
### The error may involve com.del.persistence.EmailMapper.updateEmailTasks-Inline
### The error occurred while setting parameters
### Cause: ptions.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 'update staff
SET status = 1, send_email_code='abc@abc.abc';update sta ff SET status = 2, send_email_code='test@qq' ' at line 6
; bad SQL grammar []; nested exception ptions.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 'update b_email_msg_remind
SET send_status = 1, send_email_code='abc@abc.abc'' at line 6
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持脚本之家。如有错误或未考虑完全的地⽅,望不吝赐教。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。