在 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()
方法提交事務。