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 2018-12-04 15:38  LoaderMan  阅读(3120)  评论(0编辑  收藏  举报

导航