mybatis注解开发-@Delete注解

介绍

通过 @Delete 注解执行 delete 语句

修改IUserMapper

增加下面代码:

@Delete("delete from user where id=#{id}")
public int deleteById(String id);

注解中的sql语句,与 mapper.xml 中的写法是一样的

由于只传入 一个基本数据类型,所以在sql中 #{id}可以写成任意值,如:#{aaa}也是可以的,但代码规范不允许这么写

注意: 一般都是逻辑删除,很少使用这种物理删除

测试

@org.junit.Test
public void testDelete() throws IOException {

    InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);

    int i=mapper.deleteById("1111");
    sqlSession.commit();
    System.out.println("删除"+i+"条记录!");

    sqlSession.close();
    inputStream.close();
}

原文出处:/show_1IXlJ3dIOJ.html