例子
原来
activeRecordPlugin.setBaseSqlTemplatePath(PathKit.getRootClassPath());
activeRecordPlugin.addSqlTemplate("sql/demo.sql");假如生成了一个 A.jar 文件当你运行的时候 会报错 找不到文件
因为打包成了 jar 包之后需要通过IO流才能读取,通过File对象读取不到
我给改了一下
activeRecordPlugin.setBaseSqlTemplatePath("classpath:");
activeRecordPlugin.addSqlTemplate("sql/demo.sql");然后从写了一下 FileStringSource.java 的 loadFile 方法,用了 Spring 的读取配置文件的类
示例代码
package com.jfinal.template;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public static StringBuilder loadFile(String fileName, String encoding) {
if(fileName.indexOf("classpath:") == 0) {
StringBuilder ret = new StringBuilder();
try {
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource resource = patternResolver.getResource(fileName);
ret.append(IOUtils.toString(resource.getInputStream(), encoding));
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
File file = new File(fileName);
if (!file.exists()) {
throw new RuntimeException("File not found : " + fileName);
}
StringBuilder ret = new StringBuilder((int)file.length() + 3);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
// br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
if (line != null) {
ret.append(line);
} else {
return ret;
}
while ((line=br.readLine()) != null) {
ret.append("\n").append(line);
}
return ret;
} catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
com.jfinal.kit.LogKit.error(e.getMessage(), e);
}
}
}
}
这个功能后续考虑做进去,目前建议直接修改 jfinal sql 管理模块的源码,简单粗爆