mybatis中foreach使用

insert

1
2
3
4
5
6
7
8
@Insert("<script>" +
" insert into user (uuid,name)" +
" values" +
" <foreach collection=\"userEntityList\" item = \"item\" separator=\",\" >"+
" (#{item.uuid},#{item.name} )" +
" </foreach>" +
"</script>")
int save(@Param("userEntityList") List<UserEntity> userserEntityList);

update

1
2
3
4
5
6
7
8
9
@Update("<script>" +
"update user set status = #{status} " +
" where uuid in " +
" <foreach collection=\"uuidList\" item = \"uuid\" open=\"(\" separator=\",\" close=\")\">"+
" #{uuid} " +
" </foreach>" +
"</script>")
int updateUserStatus(@Param("uuidList") List<String> uuidList,
@Param("status") Integer status);

delete

1
2
3
4
5
6
7
@Delete("<script>" +
" delete from user where uuid in " +
" <foreach collection=\"userEntityList\" item = \"item\" open=\"(\" separator=\",\" close=\")\">"+
" #{item.uuid} " +
" </foreach>" +
" </script>")
int deleteUser(@Param("userEntityList") List<UserEntity> userEntityList);

select

1
2
3
4
5
6
7
8
9
10
@Select("<script>"  +
"select name from user " +
" <where> " +
" uuid in" +
" <foreach collection=\"uuidList\" item = \"uuid\" open=\"(\" separator=\",\" close=\")\">"+
" #{uuid} " +
" </foreach>" +
" </where> " +
"</script>")
List<String> getUserNameList(List<String> uuidList );

mybatis中foreach使用
https://www.wekri.com/mybatis/mybatis-foreach/
Author
Echo
Posted on
January 8, 2021
Licensed under