|
@@ -1,7 +1,9 @@
|
|
|
package com.boman.system.mapper;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.boman.common.core.utils.obj.ObjectUtils;
|
|
|
+import com.boman.system.common.FormDataConstant;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.ibatis.annotations.*;
|
|
|
import org.apache.ibatis.annotations.Param;
|
|
@@ -89,16 +91,21 @@ public interface StandardlyMapper {
|
|
|
/**
|
|
|
* 功能描述: 自定义查询,需要查询的字段和value都在condition中
|
|
|
*
|
|
|
- * @param tableName tableName
|
|
|
- * @param condition condition
|
|
|
- * @param limit 分页
|
|
|
- * @param offset 分页
|
|
|
+ * @param tableName tableName
|
|
|
+ * @param condition condition
|
|
|
+ * @param packCondition 封装好的查询条件
|
|
|
+ * @param showData 需要查询的列
|
|
|
+ * @param orderBy orderBy
|
|
|
+ * @param limit 分页
|
|
|
+ * @param offset 分页, 可以为null
|
|
|
* @return java.util.List<com.alibaba.fastjson.JSONObject>
|
|
|
*/
|
|
|
@SelectProvider(type = StandardlyMapper.SqlProvider.class, method = "selectByCondition")
|
|
|
List<JSONObject> selectByCondition(@Param("tableName") String tableName
|
|
|
, @Param("condition") JSONObject condition
|
|
|
- , @Param("showData") JSONObject showData
|
|
|
+ , @Param("packCondition") JSONObject packCondition
|
|
|
+ , @Param("showData") JSONArray showData
|
|
|
+ , @Param("orderBy") String orderBy
|
|
|
, @Param("limit") int limit
|
|
|
, @Param("offset") int offset);
|
|
|
|
|
@@ -404,24 +411,61 @@ public interface StandardlyMapper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// @Param("tableName") String tableName, @Param("condition") JSONObject condition, @Param("limit") int limit, @Param("offset") int offset
|
|
|
public String selectByCondition(Map<String, Object> para) {
|
|
|
- JSONObject condition = (JSONObject) para.get("condition");
|
|
|
- ObjectUtils.requireNonNull(condition);
|
|
|
+// JSONObject condition = (JSONObject) para.get("condition");
|
|
|
+ JSONObject packCondition = (JSONObject) para.get("packCondition");
|
|
|
String tableName = (String) para.get("tableName");
|
|
|
- ObjectUtils.requireNonNull(tableName);
|
|
|
+ String orderBy = (String) para.get("orderBy");
|
|
|
int limit = (int) para.get("limit");
|
|
|
- ObjectUtils.requireNonNull(limit);
|
|
|
int offset = (int) para.get("offset");
|
|
|
- ObjectUtils.requireNonNull(offset);
|
|
|
-
|
|
|
- StringBuilder sql = new StringBuilder();
|
|
|
- sql.append("select ");
|
|
|
+ JSONArray showData = (JSONArray) para.get("showData");
|
|
|
+
|
|
|
+ StringBuilder wholeSql = new StringBuilder();
|
|
|
+ wholeSql.append("select ");
|
|
|
+ // showData
|
|
|
+ StringBuilder showDataSql = new StringBuilder();
|
|
|
+ for (Object columnObj : showData) {
|
|
|
+ String columnName = (String) columnObj;
|
|
|
+ showDataSql.append(columnName).append(", ");
|
|
|
+ }
|
|
|
|
|
|
+ wholeSql.append(StringUtils.substringBeforeLast(showDataSql.toString(), ","));
|
|
|
+ wholeSql.append(" from ").append(tableName);
|
|
|
+ wholeSql.append(" where ");
|
|
|
+ // 条件
|
|
|
+ StringBuilder conditionSql = new StringBuilder();
|
|
|
+ for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Object valueObj = entry.getValue();
|
|
|
+ String valueStr = ((String) valueObj);
|
|
|
+ String[] split = valueStr.split("_");
|
|
|
+ String value = split[0];
|
|
|
+ String type = split[1];
|
|
|
+ conditionSql.append(key).append(covert(type, key, value)).append(" and ");
|
|
|
+ }
|
|
|
|
|
|
+ wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
|
|
|
+ wholeSql.append(" order by ").append(orderBy).append(" limit ").append(limit);
|
|
|
+ if (ObjectUtils.isNotEmpty(offset)) {
|
|
|
+ wholeSql.append(", ").append(offset);
|
|
|
+ }
|
|
|
|
|
|
+ String result = wholeSql.toString();
|
|
|
+ LOGGER.info("查询拼出的sql语句为:{}", result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
- return "";
|
|
|
+ private String covert(String type, String key, String value) {
|
|
|
+ switch (type) {
|
|
|
+ case FormDataConstant.EQ:
|
|
|
+ return " = " + ObjectUtils.escapeStr(value);
|
|
|
+ case FormDataConstant.LIKE:
|
|
|
+ return " like " + "concat('%', #{condition." + key + "}, '%')";
|
|
|
+ default:
|
|
|
+ throw new IllegalArgumentException("参数非法");
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|