一.删除
接口方法
package com.gjw.mapper;
import com.gjw.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface EmpMapper {
// 根据ID删除数据
@Delete("delete from emp where id = #{id}")
// public void delete(Integer id);
// delete方法其实是有返回值的,其返回值代表此次操作影响的记录数
public int delete(Integer id);
}
如果将id写死,那么每次要删除新的员工信息时,就要重新修改代码,太过繁琐,因此我们使用动态获取的办法,使用mybatis当中提供的一个参数占位符“#{}”。在{}当中写上参数的名字。
测试代码
package com.gjw;
import com.gjw.mapper.EmpMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
@Autowired
private EmpMapper empMapper;
@Test
void contextLoads() {
}
@Test
public void testDelete() {
// empMapper.delete(17);
int delete = empMapper.delete(16);
System.out.println(delete); // id = 17的记录以及删除过了,因此此次delete删除操作影响的记录数为0
}
}
delete方法其实是有返回值的,它的返回值代表此次操作影响了几条记录数。