批量删除
public interface SelectMapper {
/**
* 批量删除
* @param ids
* @return
*/
int deleteMore(@Param("ids") String ids);
}
mybatis的SQL编写
<?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="com.example.mybatis.mapper.SelectMapper">
<!--int deleteMore(@Param("ids") String ids);-->
<delete id="deleteMore">
delete from t_user where id in (${ids})
</delete>
</mapper>
注意:批量删除时只能用$不能用#,否则就会报错,因为#{}会为当前所赋的值加上单引号,这就导致SQL语句异常的原因
若使用#{},在SQL编译时,所赋予的值就变成了如下
例如ids={1,2,3,4,5,6}
delete from t_user where id in (#{ids})
编译后的SQL语句为
delete from t_user where id in ('1,2,3,4,5,6')
因此导致sql异常