Mybatis 查詢前判斷表是否存在
CommonMapper.java 接口方式
@Mapper @Component public interface CommonMapper { /** * 判斷表是否存在 * * @param tableName 表名稱 * @return 結果 * @author yunnuo */ @Select(" SELECT COUNT(*) as count FROM information_schema.TABLES WHERE table_name = #{tableName}") Integer existsTable(@Param("tableName") String tableName); }
xml方式
<!-- 判斷表是否存在 --><select id="existsTable" parameterType="string" resultType="java.lang.Integer"> SELECT COUNT(*) as count FROM information_schema.TABLES WHERE table_name = #{tableName} </select>
測試
@Test public void test3(){ Integer integer = commonMapper.existsTable("data_20220601"); System.out.println(integer); }
1表示存在,0表示不存在,在做動態表名前要做表存在校驗,否則可能會報Table 'xxxx' doesn't exist!
學習時的痛苦是暫時的 未學到的痛苦是終生的
作者:卷心菜的奇妙歷險
本文版權歸作者和博客園共有,遵循 CC 4.0 BY-SA 版權協議,歡迎轉載 轉載請附上原文出處鏈接和本聲明,否則保留追究法律責任的權利。

浙公網安備 33010602011771號