Use the following as direct as possible if we want to use in the case in SQL syntax:
update user set status='1' where id in (1,2,3) ; select * from user where id in (1,2,3) ;
But if you use the MyBatis is in the operator, write it like this certainly will complain:
@Update("update user set status=#{status} where id in #{userIds}")
public void updateUserStatus(@Param("userIds") String userIds, @Param("status") int status);
Wherein userIds = ( . 1 , 2 , . 3 )
Such direct splicing of writing, it seems very simple, in findByCondition with no problem, but in a dynamic SQL MyBatis annotations are not supported.
God closes a door, it will certainly open a window.
For the above case, MyBatis provides foreach statement to achieve IN query.
foreach syntax is as follows: foreach statement, the parameter type collection property can support: List, array, map collection
collection: mapper.java must be followed in @Param label elements specified name as
item: representing each element of an alias in an iterative process, you can easily named, but it must be like in the name of the element inside of # {}.
index: represents each iteration in an iterative process to the position (subscript)
Open: prefix, sql statement must be set with parentheses () enclosed
close: suffix
separator: separator, showing the elements of each iteration between what separated
Example:
@Update({"<script>", "update user set status=#{status} where id in ", "<foreach collection=\"userIdList\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">", "#{userId}", "</foreach>", "</script>"}) public void updateUserStatus(@Param("userIdList") List<String> userIdList, @Param("status") int status);
Common learning and common progress, if any supplement, please point out, thank you!