前几天在琢磨mybatis xml热加载的问题,参考了一篇文章https://blog.csdn.net/wang1988081309/article/details/66261514,原理还是通过定时扫描xml文件去跟新,但放到项目上就各种问题,由于用了mybatisplus死活不生效。本着"即插即用"的原则,狠心把其中的代码优化了一遍,能够兼容mybatisplus,还加入了一些日志,直接上代码
package com.bzd.core.mybatis;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import com.google.common.collect.Sets;
/**
* 刷新MyBatis Mapper XML 线程
* @author ThinkGem
* @version 2016-5-29
*/
public class MapperRefresh implements java.lang.Runnable {
public static Logger log = LoggerFactory.getLogger(MapperRefresh.class);
private static String filename = "mybatis-refresh.properties";
private static Properties prop = new Properties();
private static boolean enabled; // 是否启用Mapper刷新线程功能
private static boolean refresh; // 刷新启用后,是否启动了刷新线程
private Set<String> location; // Mapper实际资源路径
private Resource[] mapperLocations; // Mapper资源路径
private Configuration configuration; // MyBatis配置对象
private Long beforeTime = 0L; // 上一次刷新时间
private static int delaySeconds; // 延迟刷新秒数
private static int sleepSeconds; // 休眠时间
private static String mappingPath; // xml文件夹匹配字符串,需要根据需要修改
static {
// try {
// prop.load(MapperRefresh.class.getResourceAsStream(filename));
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println("Load mybatis-refresh “"+filename+"” file error.");
// }
URL url = MapperRefresh.class.getClassLoader().getResource(filename);
InputStream is;
try {
is = url.openStream();
if (is == null) {
log.warn("applicationConfig.properties not found.");
} else {
prop.load(is);
}
} catch (IOException e) {
e.printStackTrace();
}
String value = getPropString("enabled");
System.out.println(value);
enabled = "true".equalsIgnoreCase(value);
delaySeconds = getPropInt("delaySeconds");
sleepSeconds = getPropInt("sleepSeconds");
//mappingPath = getPropString("mappingPath");
delaySeconds = delaySeconds == 0 ? 50 : delaySeconds;
sleepSeconds = sleepSeconds == 0 ? 3 : sleepSeconds;
mappingPath = StringUtils.isBlank(mappingPath) ? "mappings" : mappingPath;
log.debug("[enabled] " + enabled);
log.debug("[delaySeconds] " + delaySeconds);
log.debug("[sleepSeconds] " + sleepSeconds);
log.debug("[mappingPath] " + mappingPath);
}
public static boolean isRefresh() {
return refresh;
}
public MapperRefresh(Resource[] mapperLocations, Configuration configuration) {
this.mapperLocations = mapperLocations;
this.configuration = configuration;
}
@Override
public void run() {
beforeTime = System.