From 9c914bf8219a9d059192b53e4ecd222a00a7fe4e Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期三, 04 九月 2019 23:30:05 +0800
Subject: [PATCH] 动态java 类开发中
---
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java | 61 +++++++++++++++++++++++++-----
1 files changed, 50 insertions(+), 11 deletions(-)
diff --git a/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java b/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
index 28688d1..0d13cd5 100644
--- a/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
+++ b/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
@@ -16,7 +16,13 @@
import com.java110.entity.service.ServiceSql;
import com.java110.service.dao.IQueryServiceDAO;
import com.java110.service.smo.IQueryServiceSMO;
+import javassist.ClassPool;
import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.ognl.Ognl;
+import org.apache.ibatis.ognl.OgnlException;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -186,11 +192,14 @@
param = param.substring(0, param.length() - 1);
}*/
+
+
+
dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
"鎴愬姛", JSONObject.parseObject(interpreter.eval("execute(" + dataQuery + ")").toString())));
} catch (Exception e) {
logger.error("鏁版嵁浜や簰寮傚父锛�", e);
- throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父,"+e.getMessage());
+ throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父," + e.getMessage());
}
}
@@ -252,7 +261,7 @@
String currentSql = sqlObj.getString(dataQuery.getTemplateKey());
//澶勭悊 if 鍒ゆ柇
- currentSql = dealSqlIf(currentSql);
+ currentSql = dealSqlIf(currentSql, params);
String[] sqls = currentSql.split("#");
String currentSqlNew = "";
@@ -325,22 +334,52 @@
/**
* 澶勭悊SQL璇彞
- * @param oldSql
- * select * from s_a a
- * where if(name != null && name != ''){
- * a.name = #name#
- * }
+ *
+ * @param oldSql select * from s_a a
+ * where <if test="name != null && name != ''">
+ * a.name = #name#
+ * </if>
* @return
*/
- private String dealSqlIf(String oldSql){
- String newSql = "";
+ public String dealSqlIf(String oldSql, JSONObject requestParams) throws DocumentException, OgnlException {
+ StringBuffer newSql = new StringBuffer();
+ String tmpSql = "";
+ Boolean conditionResult = false;
// 鏈寘鍚� 鏉′欢璇彞
- if(!oldSql.replace(" ","").contains("if(")){
+ if (!oldSql.contains("<if")) {
return oldSql;
}
+ String[] oSqls = oldSql.split("</if>");
+ for (String oSql : oSqls) {
+ if (!oSql.startsWith("<if")) {
+ newSql.append(oSql.substring(0, oSql.indexOf("<if")));
+ }
- return newSql;
+ tmpSql = oSql.substring(oSql.indexOf("<if")) + "</if>";
+
+ Element root = DocumentHelper.parseText(tmpSql).getRootElement();
+
+ String condition = root.attribute("test").getValue();
+
+ Object condObj = Ognl.parseExpression(condition);
+
+ Object value = Ognl.getValue(condObj, requestParams);
+
+ if (value instanceof Boolean) {
+ conditionResult = (Boolean) value;
+ } else {
+ throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "閰嶇疆閿欒锛宨f璇彞閰嶇疆閿欒 " + condition);
+ }
+
+ if (conditionResult) {
+ newSql.append(root.getText());
+ }
+
+ }
+
+
+ return newSql.toString();
}
--
Gitblit v1.8.0