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!

 

posted @   卷心菜的奇妙历险  阅读(4666)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2020-06-01 自己的书单 --更新中
点击右上角即可分享
微信分享提示