本地mysql版本 5.7.26:
1、首先在连接数据库的时候需要添加参数
allowMultiQueries=true
因为默认是只允许批量修改一条的,需要改为true;
2、java后端dao层使用的mybatis框架,Mapper接口的方法为:
int updateBatch(List<MyPojo> myPojoList);
3、配置文件xml中
<update id="updateBatch" parameterType="java.util.List" >
<foreach collection="list" item="myPojo" index="index" open="" close="" separator=";">
update
my_table
set
name= #{myPojo.name}
,age= #{myPojo.age}
,is_able = #{myPojo.isAble}
,create_time = #{myPojo.createTime}
,create_by = #{myPojo.createBy}
,update_time = #{myPojo.updateTime}
,update_by = #{myPojo.updateBy}
,remark = #{myPojo.remark}
where
id= #{myPojo.id}
</foreach>
</update>
4、正常情况下就可以实现了。
出现的问题:本地运行没有问题,上传到服务器会报错如下:
check the manual that corresponds to your MySQL server version for the right syntax to use near "update my_table set name='测试姓名'"
检查后发现服务器的mysql版本是5.6,换成5.7.26 问题解决;