/**
* 持久层
*/
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>