在MyBatis中清空表數據的方法有兩種:
<delete id="truncateTable">
TRUNCATE TABLE table_name;
</delete>
然后在Java代碼中調用這個SQL語句:
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
sqlSession.delete("truncateTable");
sqlSession.commit();
} finally {
sqlSession.close();
}
<delete id="deleteAll">
DELETE FROM table_name;
</delete>
然后在Java代碼中調用這個SQL語句:
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
sqlSession.delete("deleteAll");
sqlSession.commit();
} finally {
sqlSession.close();
}
這兩種方法都可以用來清空表數據,選擇哪種方法取決于具體的需求和情況。