springboot mybatis注解方式批量insert和使用in语句查询

1、批量插入

    @Insert({
            "<script>",
            "insert into sys_user_role(user_id,role_id) values ",
            "<foreach collection='roles' item='item' index='index' separator=','>",
            "(#{item.userId}, #{item.roleId})",
            "</foreach>",
            "</script>"
    })
    int insertByBatch(@Param(value = "roles") List<SysUserRole> roles);

通过@Param指定集合参数,item为集合内每个对象,index为集合自然序号

2、使用in语句查询

    @Select({
            "<script>",
            "select count(0) from sys_role where id in ",
            "<foreach collection='roleIds' item='item' index='index' open='(' separator=',' close=')'>",
            "(#{item})",
            "</foreach>",
            "</script>"
    })
    Integer checkRoleId(@Param(value = "roleIds") List<Long> roleIds);

查询要特别指定开闭的左右括号

猜你喜欢

转载自www.cnblogs.com/asker009/p/12740290.html
今日推荐