在 MyBatis 中执行删除操作通常是通过使用 delete
标签来实现的,具体步骤如下:
<delete id="deleteUserById" parameterType="int">
DELETE FROM user
WHERE id = #{id}
</delete>
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUserById(1);
sqlSession.commit();
} finally {
sqlSession.close();
}
在上面的示例中,我们首先通过 sqlSession.getMapper(UserMapper.class)
方法获取了 Mapper 接口的实例,然后调用了 deleteUserById
方法来执行删除操作,最后通过 sqlSession.commit()
方法提交事务。