2018-12-21 11:08
加一个BaseModel, 通过堆栈获取信息
/**
* 获取sql模板语句
* @param data
* @return
*/
public SqlPara getSqlPara(Map data) {
StackTraceElement[] stacks = (new Throwable()).getStackTrace();
SqlPara sqlPara = _getConfig().getSqlKit().getSqlPara(getSqlParaKey(stacks), data);
if (sqlPara == null) {
sqlPara = _getConfig().getSqlKit().getSqlPara(getSqlParaKeyTwo(stacks), data);
}
return sqlPara;
}
private String getSqlParaKey(StackTraceElement[] stacks) {
/*Parameter[] parameters = null;*/
String[] classNames = stacks[1].getClassName().split("\\.");
String className = StrUtil.removeAll(classNames[classNames.length - 1], "Dao");
String methodName = stacks[1].getMethodName();
if (StrUtil.containsIgnoreCase(methodName, "ByModel")) {
methodName = "queryByModel";
}
/* for (Method method: ClassLoaderUtil.loadClass(stacks[1].getClassName()).getMethods()) {
if (method.getName().equals(methodName)) {
parameters = method.getParameters();
break;
}
}*/
String key = StrKit.firstCharToLowerCase(className) + "." + methodName;
return key;
}
private String getSqlParaKeyTwo(StackTraceElement[] stacks) {
String[] classNames = stacks[1].getClassName().split("\\.");
String className = StrUtil.removeAll(classNames[classNames.length - 1], "ServiceImpl");
String methodName = stacks[1].getMethodName();
if (StrUtil.containsIgnoreCase(methodName, "ByModel")) {
methodName = "queryByModel";
}
String key = StrKit.firstCharToLowerCase(className) + "." + methodName;
return key;
}