Mybatis循环操作

批量插入
insert into user(user_id,name) values (1,'test0'),(2,'test1');

insert into
user(
user_id,name
)
values
(#{item.userId},#{item.name})

批量删除
delete from user where user_id in(1,2);

delete from user
where
user_id in
#{item}


批量更新
①:循环更新,连接数据库N次,效率低

update user set name = 'test0' where user_id=1;update user set name = 'test1' where user_id =2;

update user
name=#{name}
where user_id = #{item.userId}

②:循环更新,连接数据库一次,效率较高(case when的两种写法)
写法一:
update user set name = case user_id when 1 then 'test0' when 2 then 'test1' else name end where user_id in(1,2)

update user set name=
when #{item.userId} then #{item.name}
where user_id in
#{item.user_id}

写法二:

update user set name = case when user_id = 1 then 'test0' when user_id = 2 then 'test1' else name end where user_id in(1,2)

update user
                 set name =
             
when user_id = #{item.userId} then #{item.name}
             where id in
                
                 #{item.userId}
                


你可能感兴趣的:(mybatis)