zoukankan      html  css  js  c++  java
  • mybatis检测mysql表是否存在

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

    mapper:

    import java.util.Map;
    
    import org.apache.ibatis.annotations.Param;
    
    /**
     * 通用的mapper
     * @author yangzl
     * @data 2019年4月8日
     *
     */
    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.yangzhilong.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.yangzhilong.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 = "ad_engine";
        @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;
        }
    }
  • 相关阅读:
    cestos7安装zookeeper
    Cestos7安装Elasticsearch5.4.3
    debian使用nginx创建静态文件存储
    tomcat优化
    tomcat停止和启动脚本
    linux中shell变量$#,$@,$0,$1,$2的含义解释
    redis设置bind
    Jenkins send build artifacts over ssh配置
    nginx 负载均衡5种配置方式
    Jenkins tomcat打包启动脚本,待完善
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/10669775.html
Copyright © 2011-2022 走看看