Mybatis检测mysql表是否存在

3 篇文章 0 订阅

原文1
原文2

1、优先使用information_schema来检查,如果没有查询这个的权限则使用show tables来检查。

mapper:

package com.chenp.demo.dao;

import java.util.Map;
import org.apache.ibatis.annotations.Param;
 
/**
 * 通用的mapper
 * @author cp218
 * @data 2022/12/9 15:57
 *
 */
public interface CommonMapper {
	
    /**
     * 使用information_schema检查表是否存在
     * @param tableSchema
     * @param tableName
     * @return
     */
    Integer checkTableExistsWithSchema(@Param("tableSchema")String tableSchema, @Param("tableName")String tableName);
 
    /**
     * 使用show tables检查表是否存在
     * @param tableName
     * @return
     */
    Map<String, String> checkTableExistsWithShow(@Param("tableName")String tableName);
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chenp.demo.mapper.CommonMapper">

    <select id="checkTableExistsWithSchema"
        resultType="java.lang.Integer">
        SELECT 
        	COUNT(1) 
        FROM information_schema.tables 
        WHERE
        	table_schema = #{tableSchema} 
        	AND table_name = #{tableName}
    </select>
 
    <select id="checkTableExistsWithShow"
        resultType="java.util.Map">
        show tables like #{tableName}
    </select>
    
</mapper>

通用service:

package com.chenp.demo.service;
 
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.yangzhilong.mapper.CommonMapper;
 
import lombok.extern.slf4j.Slf4j;
 
@Service
@Slf4j
public class CommonService {
	
    private static final String TABLE_SCHEMA = "cp_user";
    @Autowired
    private CommonMapper commonMapper;
 
    /**
     * 检查表是否存在
     * @param tableName
     * @return
     */
    public boolean checkTableExists(String tableName) {
        try {
            Integer count = commonMapper.checkTableExistsWithSchema(TABLE_SCHEMA, tableName);
            return count == 1;
        } catch (Exception e) {
            log.error("使用information_schema检测表失败", e);
            Map<String, String> list = commonMapper.checkTableExistsWithShow(tableName);
            if(!CollectionUtils.isEmpty(list)) {
                return true;
            }
        }
 
        return false;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,可以使用XML配置文件来定义SQL语句和映射关系。引用\[1\]和引用\[2\]是两个示例的XML配置文件,它们都定义了一个名为"checkTableExistsWithSchema"的查询语句和一个名为"checkTableExistsWithShow"的查询语句。这两个查询语句都用于检查是否存在。 在这两个示例中,查询语句的具体实现是通过调用CommonMapper接口中的方法来完成的。引用\[3\]是CommonMapper接口的定义,其中包含了两个方法:checkTableExistsWithSchema和checkTableExistsWithShow。这两个方法分别接受的模式和名作为参数,并返回相应的结果。 因此,要在MyBatis中查询是否存在,可以使用CommonMapper接口中的checkTableExistsWithSchema或checkTableExistsWithShow方法,并传入相应的模式和名作为参数。这样就可以得到一个Integer类型的结果或一个Map类型的结果,用于判断是否存在。 #### 引用[.reference_title] - *1* [mybatis检测mysql是否存在](https://blog.csdn.net/weixin_30730151/article/details/99452328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Mybatis检测mysql是否存在](https://blog.csdn.net/Mr_Chp/article/details/129180950)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值