时间:2021-07-01 10:21:17 帮助过:4人阅读
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; } }
mybatis检测mysql表是否存在
标签:list service erro sele param mon lse nal log