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为集合自然序号
比较一下用xml文件的方式:
<insert id="insertBatch"> INSERT INTO 表名( `字段名1`, `字段名2`, `字段...`) VALUES <foreach collection="list" item="item" separator="," open="(" close=")"> #{item.字段1},#{item.字段2},#{item中的每一个字段名...} </foreach> </insert>
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);
查询要特别指定开闭的左右括号