mybatis的foreach 删除多条使用汇总

J2EE 码拜 6年前 (2015-07-11) 301次浏览 0个评论
 

@Test //删除多条-数组
public void testDeleteByArray(){
SqlSession session = factory.openSession();
int[] ids = {4,6};
session.delete(“cn.qq.mapper.PersonMapper.deleteArray”, ids);
session.commit();
}

@Test //删除多条-LIST
public void testDeleteByList(){
SqlSession session = factory.openSession();
List<Integer> _list = new ArrayList<Integer>();
_list.add(4);
_list.add(6);

session.delete(“cn.qq.mapper.PersonMapper.deleteList”, _list);
session.commit();
}

@Test //删除多条 MAP
public void testDeleteByMap(){
SqlSession session = factory.openSession();
Map<String,Object> paraMap = new HashMap<String,Object>();
int[] ids = {4,6};
paraMap.put(“ids”, ids);

session.delete(“cn.qq.mapper.PersonMapper.deleteMap”, paraMap);
session.commit();
}

<!– 删除多条,整形数组 –>
<delete id=”deleteArray” parameterType=”int”>
delete from person
where id in
(
<foreach collection=”array” item=”id” separator=”,”>
#{id}
</foreach>
)
</delete>

<!– 删除多条,LIST集合 –>
<delete id=”deleteList” parameterType=”int”>
delete from person
where id in
<foreach collection=”list” item=”id” open=”(” close=”)” separator=”,”>
#{id}
</foreach>
</delete>

<!– 删除多条,Map , ids 代表map中key –>
<delete id=”deleteMap” parameterType=”map”>
delete from person
where id in
<foreach collection=”ids” item=”id” open=”(” close=”)” separator=”,”>
#{id}
</foreach>
</delete>


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明mybatis的foreach 删除多条使用汇总
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!