mybatis之动态SQL操作之删除

复制代码
/**
 * 持久层
 */
public class StudentDao {
    /**
     * 动态SQL--删除
     */
    public void dynaSQLwithDelete(int... ids) throws Exception{
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try{
            sqlSession.delete("mynamespace.dynaSQLwithDelete",ids);
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            sqlSession.commit();
            MyBatisUtil.closeSqlSession();
        }
    }
    public static void main(String[] args) throws Exception{
        StudentDao dao = new StudentDao();
        dao.dynaSQLwithDelete(1,3,5,7);
    }
}
复制代码

StudentMapper.xml

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="mynamespace">
    <!-- item表示迭代的参数 -->
    <delete id="dynaSQLwithDelete">
        delete from students where id in
        <!-- 
        <foreach collection="array" open="(" close=")" separator="," item="ids">
            ${ids}
        </foreach>
        -->    
        <foreach collection="list" open="(" close=")" separator="," item="ids">
            ${ids}
        </foreach>    
    </delete>    
</mapper>
复制代码

 

posted on   LoaderMan  阅读(3127)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示