delete 标签是用于定义delete 语句的.
delete 有几个常用的属性, 但是通常只需要设置id 即可.
id: sql 片段在命名空间内的唯一标识. 和mapper 中方法名保持一致
parameterType: 参数类型, 通常都可以省略.
flushCache: 是否刷新(清空)一级缓存和二级缓存, 默认为true. 笔者尝试设置为false,并不生效. 所以使用默认即可.
timeout: sql 执行超时时间, 默认未设置, 由数据库驱动决定.
statementType: 执行sql时使用的statement类型, 默认为PREPARED. 可选值为:STATEMENT,PREPARED 或 CALLABLE 的一个
Mapper中方法定义:
// 返回值为空
void delete(Long id);
// 返回值为布尔类型
boolean delete(Long id);
// 返回值为删除的条数
long delete(Long id);
sql 片段:
delete from t_employee where id = #{id}
delete from t_employee where id = #{id}
delete from t_employee
where id in
#{id}
public interface EmployeeMapper {
// 删除, 返回是否删除
boolean delete(Long id);
// 批量删除, 返回成功删除的个数
long batchDelete(List ids);
}
// 测试删除方法
@Test
public void delete(){
EmployeeMapper employeeMapper = SqlSessionUtil.getMapper(EmployeeMapper.class, true);
boolean flag = employeeMapper.delete(9L);
Assert.assertEquals(true, flag);
}
// 测试批量删除
@Test
public void batchDelete(){
EmployeeMapper employeeMapper = SqlSessionUtil.getMapper(EmployeeMapper.class, true);
List idList = Arrays.asList(1L, 2L, 3L, 4L, 5L);
long cnt = employeeMapper.batchDelete(idList);
Assert.assertEquals(5L, cnt);
}