|
@@ -20,101 +20,84 @@ import static com.boman.domain.constant.FormDataConstant.*;
|
|
|
|
|
|
/**
|
|
|
* @author shiqian
|
|
|
- * @description
|
|
|
+ * @description 通用的mapper
|
|
|
* @date 2021年03月22日 11:36
|
|
|
**/
|
|
|
@Mapper
|
|
|
@Component
|
|
|
-public interface StandardlyMapper {
|
|
|
+public interface StandardMapper {
|
|
|
|
|
|
- Logger LOGGER = LoggerFactory.getLogger(StandardlyMapper.class);
|
|
|
+ Logger LOGGER = LoggerFactory.getLogger(StandardMapper.class);
|
|
|
|
|
|
|
|
|
- /** {@link SqlProvider#update(java.util.Map)} */
|
|
|
- @UpdateProvider(type = SqlProvider.class, method = "update")
|
|
|
- int updateById(@Param("tableName") String var1, @Param("model") JSONObject var2
|
|
|
- , @Param("pkName") String pkName, @Param("ids") Long[] ids);
|
|
|
-
|
|
|
- @UpdateProvider(
|
|
|
- type = SqlProvider.class,
|
|
|
- method = "update"
|
|
|
- )
|
|
|
- int newUpdateById(@Param("tableName") String var1, @Param("model") long var2);
|
|
|
-
|
|
|
-
|
|
|
- @DeleteProvider(
|
|
|
- type = SqlProvider.class,
|
|
|
- method = "delete"
|
|
|
- )
|
|
|
- int deleteByIds(@Param("tableName") String var1, @Param("ids") Long[] var2, @Param("pkName") String pkName);
|
|
|
-
|
|
|
- @Delete({"DELETE FROM ${tableName} where ID = #{id}"})
|
|
|
- int deleteById(@Param("tableName") String tableName, @Param("pkName") String pkName, @Param("id") long var2);
|
|
|
-
|
|
|
- @Select({"select * FROM ${tableName} where ID = #{id}"})
|
|
|
- JSONObject getById(@Param("tableName") String var1, @Param("id") long var2);
|
|
|
-
|
|
|
- @Select({"select ${columns} FROM ${tableName} where ID = #{id}"})
|
|
|
- JSONObject getColumnsById(@Param("tableName") String var1, @Param("columns") String var2, @Param("id") long var3);
|
|
|
-
|
|
|
- @InsertProvider(
|
|
|
- type = SqlProvider.class,
|
|
|
- method = "insert"
|
|
|
- )
|
|
|
+ /* ***************************************************** insert ******************************************************/
|
|
|
+ /** {@link SqlProvider#insert(Map)} */
|
|
|
+ @InsertProvider(type = SqlProvider.class, method = "insert")
|
|
|
int insert(@Param("tableName") String var1, @Param("model") JSONObject var2);
|
|
|
|
|
|
/** {@link SqlProvider#insertList(Map)} */
|
|
|
- @InsertProvider(type = SqlProvider.class,method = "insertList")
|
|
|
+ @InsertProvider(type = SqlProvider.class, method = "insertList")
|
|
|
int insertList(@Param("tableName") String tableName, @Param("dataList") List<JSONObject> dataList);
|
|
|
|
|
|
- @InsertProvider(
|
|
|
- type = SqlProvider.class,
|
|
|
- method = "inserts"
|
|
|
- )
|
|
|
- int inserts(@Param("tableName") String var1, @Param("models") JSONObject[] var2, @Param("processor") Consumer<JSONObject> var3);
|
|
|
|
|
|
- @Select({"select count(1) FROM ${tableName} where ${column} = #{value} limit 1"})
|
|
|
- boolean exists(@Param("tableName") String var1, @Param("column") String var2, @Param("value") Object var3);
|
|
|
+ /* ***************************************************** delete ******************************************************/
|
|
|
+ /** {@link SqlProvider#deleteByIdList(java.util.Map)} */
|
|
|
+ @DeleteProvider(type = SqlProvider.class, method = "deleteByIdList")
|
|
|
+ int deleteByIdList(@Param("tableName") String var1, @Param("idList") List<Long> idList, @Param("pkName") String pkName);
|
|
|
|
|
|
- @Select({"select count(1) FROM ${tableName} where ID = #{id} limit 1"})
|
|
|
- boolean existsById(@Param("tableName") String var1, @Param("id") long var2);
|
|
|
-
|
|
|
- @Select({"select count(1) FROM ${tableName} where id <> #{id} and ${column} = #{value} limit 1"})
|
|
|
- boolean existsWithoutId(@Param("tableName") String var1, @Param("id") long var2, @Param("column") String var4, @Param("value") Object var5);
|
|
|
+ /**
|
|
|
+ * 功能描述: 通用删除数据 {@link SqlProvider#delete(java.util.Map)}
|
|
|
+ *
|
|
|
+ * @param tableName tableName
|
|
|
+ * @param condition condition
|
|
|
+ * @param packColCondition packColCondition
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ @SelectProvider(type = SqlProvider.class, method = "delete")
|
|
|
+ Integer delete(@Param("tableName") String tableName, @Param("condition") JSONObject condition
|
|
|
+ , @Param("packColCondition") JSONObject packColCondition);
|
|
|
|
|
|
- @UpdateProvider(
|
|
|
- type = SqlProvider.class,
|
|
|
- method = "updates"
|
|
|
- )
|
|
|
- int updates(@Param("tableName") String var1, @Param("models") Collection<Object> var2, @Param("processor") Consumer<JSONObject> var3);
|
|
|
|
|
|
- /** {@link SqlProvider#updateByIdList(Map)} */
|
|
|
+ /* ***************************************************** update ******************************************************/
|
|
|
+ /** {@link SqlProvider#updateByIdList(Map)} */
|
|
|
@UpdateProvider(type = SqlProvider.class, method = "updateByIdList")
|
|
|
int updateByIdList(@Param("tableName") String tableName, @Param("pkName") String pkName
|
|
|
, @Param("idList") List<Long> idList, @Param("model") JSONObject models);
|
|
|
|
|
|
+ /** {@link SqlProvider#updateByForeignKey(Map)} */
|
|
|
@UpdateProvider(type = SqlProvider.class, method = "updateByForeignKey")
|
|
|
int updateByForeignKey(@Param("tableName") String tableName, @Param("foreignKeyName") String foreignKeyName
|
|
|
, @Param("foreignKeyList") List<Long> foreignKeyList, @Param("model") JSONObject models);
|
|
|
|
|
|
- @Select("select id from ${tableName} where ${akColumnName} = #{akColumnValue}")
|
|
|
- Long selectIdByAkColumn(@Param("tableName") String tableName, @Param("akColumnName") String akColumnName, @Param("akColumnValue") String akColumnValue);
|
|
|
+ /**
|
|
|
+ * 功能描述: 通用修改 {@link SqlProvider#update(java.util.Map)}
|
|
|
+ *
|
|
|
+ * @param tableName tableName
|
|
|
+ * @param packCommitData packCommitData
|
|
|
+ * @param packColCondition packColCondition
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ @SelectProvider(type = SqlProvider.class, method = "delete")
|
|
|
+ int update(@Param("tableName") String tableName, @Param("packCommitData") JSONObject packCommitData
|
|
|
+ , @Param("packColCondition") JSONObject packColCondition);
|
|
|
|
|
|
- @Select("select ifnull(max(${pkName}),0) from ${tableName}")
|
|
|
+
|
|
|
+ /* ***************************************************** select ******************************************************/
|
|
|
+ @Select("select max(${pkName} from ${tableName}")
|
|
|
int selectMaxId(@Param("tableName") String tableName, @Param("pkName") String pkName);
|
|
|
|
|
|
/**
|
|
|
* 功能描述: 根据id查所有
|
|
|
+ * {@link SqlProvider#selectById(Map)}
|
|
|
*
|
|
|
* @param tableName 表名
|
|
|
* @param pkName 主键名称
|
|
|
* @param id 主键值
|
|
|
+ * @param other other is empty => select * from table where id = #{id}
|
|
|
* @return com.alibaba.fastjson.JSONObject
|
|
|
*/
|
|
|
-// @Select("select * from ${tableName} where ${pkName} = #{id} limit 1")
|
|
|
@SelectProvider(type = SqlProvider.class, method = "selectById")
|
|
|
- JSONObject selectById(@Param("tableName") String tableName
|
|
|
- , @Param("pkName") String pkName, @Param("id") Long id, @Param("other") String other);
|
|
|
+ JSONObject selectById(@Param("tableName") String tableName, @Param("pkName") String pkName, @Param("id") Long id, @Param("other") String other);
|
|
|
|
|
|
/**
|
|
|
* 功能描述: 根据id查所有
|
|
@@ -128,15 +111,15 @@ public interface StandardlyMapper {
|
|
|
*/
|
|
|
@SelectProvider(type = SqlProvider.class, method = "selectByIdList")
|
|
|
List<JSONObject> selectByIdList(@Param("tableName") String tableName
|
|
|
- , @Param("pkName") String pkName, @Param("idList") List<Long> idList
|
|
|
- , @Param("showData") List<String> showData);
|
|
|
+ , @Param("pkName") String pkName
|
|
|
+ , @Param("idList") List<Long> idList
|
|
|
+ , @Param("showData") List<String> showData);
|
|
|
|
|
|
/**
|
|
|
* 功能描述: 自定义查询,需要查询的字段和value都在condition中
|
|
|
* {@link SqlProvider#selectByCondition(Map)}
|
|
|
*
|
|
|
* @param tableName tableName
|
|
|
- * @param condition condition
|
|
|
* @param packCondition 封装好的查询条件
|
|
|
* @param showData 需要查询的列
|
|
|
* @param orderBy orderBy
|
|
@@ -146,12 +129,11 @@ public interface StandardlyMapper {
|
|
|
*/
|
|
|
@SelectProvider(type = SqlProvider.class, method = "selectByCondition")
|
|
|
List<JSONObject> selectByCondition(@Param("tableName") String tableName
|
|
|
- , @Param("condition") JSONObject condition
|
|
|
- , @Param("packCondition") JSONObject packCondition
|
|
|
- , @Param("showData") List<String> showData
|
|
|
- , @Param("orderBy") String orderBy
|
|
|
- , @Param("limit") int limit
|
|
|
- , @Param("offset") int offset);
|
|
|
+ , @Param("packCondition") JSONObject packCondition
|
|
|
+ , @Param("showData") List<String> showData
|
|
|
+ , @Param("orderBy") String orderBy
|
|
|
+ , @Param("limit") int limit
|
|
|
+ , @Param("offset") int offset);
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -159,174 +141,40 @@ public interface StandardlyMapper {
|
|
|
* {@link SqlProvider#countByCondition(Map)}
|
|
|
*
|
|
|
* @param tableName tableName
|
|
|
- * @param condition condition
|
|
|
* @param packCondition 封装好的查询条件
|
|
|
* @return int
|
|
|
*/
|
|
|
@SelectProvider(type = SqlProvider.class, method = "countByCondition")
|
|
|
- int countByCondition(@Param("tableName") String tableName
|
|
|
- , @Param("condition") JSONObject condition
|
|
|
- , @Param("packCondition") JSONObject packCondition);
|
|
|
+ int countByCondition(@Param("tableName") String tableName, @Param("packCondition") JSONObject packCondition);
|
|
|
|
|
|
/**
|
|
|
* 功能描述: 根据tableName和map(属性名和属性值)进行查查
|
|
|
* {@link SqlProvider#getByMap(java.util.Map)}
|
|
|
*
|
|
|
* @param tableName tableName
|
|
|
- * @param condition 属性名和属性值
|
|
|
* @param packCondition 属性名和属性值 条件
|
|
|
* @param limitOne true sql加上limit 1, false 则不加
|
|
|
* @return java.util.List<com.alibaba.fastjson.JSONObject>
|
|
|
*/
|
|
|
@SelectProvider(type = SqlProvider.class, method = "getByMap")
|
|
|
- List<JSONObject> getByMap(@Param("tableName") String tableName
|
|
|
- , @Param("condition") JSONObject condition
|
|
|
- , @Param("packCondition") JSONObject packCondition
|
|
|
+ List<JSONObject> getByMap(@Param("tableName") String tableName, @Param("packCondition") JSONObject packCondition
|
|
|
, @Param("limitOne") boolean limitOne);
|
|
|
|
|
|
/**
|
|
|
* 功能描述: 获取最新的一条数据 create desc
|
|
|
*
|
|
|
* @param tableName tableName
|
|
|
- * @return jcom.alibaba.fastjson.JSONObject
|
|
|
+ * @return com.alibaba.fastjson.JSONObject
|
|
|
*/
|
|
|
@Select("select * from ${tableName} order by create_time desc limit 1")
|
|
|
JSONObject getNewest(@Param("tableName") String tableName);
|
|
|
|
|
|
+
|
|
|
+ /* ***************************************************** SqlProvider ******************************************************/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
class SqlProvider {
|
|
|
- static final String[] READONLY_COLUMNS = new String[]{"OWNERID", "OWNERNAME", "OWNERENAME", "CREATIONDATE", "ID"};
|
|
|
-
|
|
|
- public SqlProvider() {
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isReadOnly(String column) {
|
|
|
- String[] var1 = READONLY_COLUMNS;
|
|
|
- int var2 = var1.length;
|
|
|
-
|
|
|
- for (int var3 = 0; var3 < var2; ++var3) {
|
|
|
- String readonlyColumn = var1[var3];
|
|
|
- if (readonlyColumn.equals(column)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isNullOrEmpty(String str) {
|
|
|
- return str == null || str.length() == 0 || str.trim().length() == 0;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isNullOrEmpty(JSONObject json) {
|
|
|
- return json == null || json.isEmpty();
|
|
|
- }
|
|
|
-
|
|
|
- private static void keyToUpper(JSONObject json) {
|
|
|
- String[] keys = new String[json.size()];
|
|
|
- json.keySet().toArray(keys);
|
|
|
- String[] var2 = keys;
|
|
|
- int var3 = keys.length;
|
|
|
-
|
|
|
- for (int var4 = 0; var4 < var3; ++var4) {
|
|
|
- String key = var2[var4];
|
|
|
- if (!key.equals(key.toUpperCase())) {
|
|
|
- json.put(key.toUpperCase(), json.get(key));
|
|
|
- json.remove(key);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public String update(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- JSONObject model = (JSONObject) para.get("model");
|
|
|
- String pkName = (String) para.get("pkName");
|
|
|
- Long[] ids = (Long[]) para.get("ids");
|
|
|
- if (StringUtils.isBlank(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (!model.isEmpty()) {
|
|
|
- keyToUpper(model);
|
|
|
- SQL sql = new SQL();
|
|
|
- sql.UPDATE(tableName);
|
|
|
-
|
|
|
- // 拼装set
|
|
|
- model.forEach((key, value)->{
|
|
|
- if (!isReadOnly(key)) {
|
|
|
- // 此处需要对value加一个""
|
|
|
- sql.SET(key + " = \"" + value + "\"");
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- // 拼装in
|
|
|
- StringBuilder inSql = new StringBuilder();
|
|
|
- for (int i = 0; i < ids.length; ++i) {
|
|
|
- inSql.append(String.format("#{ids[%d]}", i));
|
|
|
- inSql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.WHERE(pkName + " in (" + inSql.toString());
|
|
|
- String wholeSql = StringUtils.substringBeforeLast(sql.toString(), ",") + "))";
|
|
|
- LOGGER.info("更改的sql语句为: {} \r\n 更改的数据为: {}", wholeSql, model);
|
|
|
- return wholeSql;
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String inserts(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- JSONObject[] models = (JSONObject[]) ((JSONObject[]) para.get("models"));
|
|
|
- Consumer<JSONObject> processor = (Consumer) para.get("processor");
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (models != null && models.length != 0) {
|
|
|
- Set<String> columns = new HashSet();
|
|
|
-
|
|
|
- for (int i = 0; i < models.length; ++i) {
|
|
|
- JSONObject model = models[i];
|
|
|
- if (model == null) {
|
|
|
- models[i] = model = new JSONObject();
|
|
|
- }
|
|
|
-
|
|
|
- if (processor != null) {
|
|
|
- processor.accept(model);
|
|
|
- }
|
|
|
-
|
|
|
- keyToUpper(model);
|
|
|
- columns.addAll(model.keySet());
|
|
|
- }
|
|
|
-
|
|
|
- StringBuffer sql = new StringBuffer();
|
|
|
- sql.append("INSERT INTO ");
|
|
|
- sql.append(tableName);
|
|
|
- sql.append("(");
|
|
|
- sql.append(String.join(",", columns));
|
|
|
- sql.append(") VALUES");
|
|
|
-
|
|
|
- for (int i = 0; i < models.length; ++i) {
|
|
|
- sql.append("(");
|
|
|
-
|
|
|
- for (Iterator var8 = columns.iterator(); var8.hasNext(); sql.append(",")) {
|
|
|
- String column = (String) var8.next();
|
|
|
- if (models[i].containsKey(column)) {
|
|
|
- sql.append(String.format("#{models[%d].%s}", i, column));
|
|
|
- } else {
|
|
|
- sql.append("default");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- sql.setCharAt(sql.length() - 1, ')');
|
|
|
- sql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- return sql.toString();
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
+ /* ***************************************************** insert ******************************************************/
|
|
|
public String insert(Map<String, Object> para) {
|
|
|
JSONObject model = (JSONObject) para.get("model");
|
|
|
String tableName = (String) para.get("tableName");
|
|
@@ -384,6 +232,46 @@ public interface StandardlyMapper {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+ /* ***************************************************** delete ******************************************************/
|
|
|
+ public String delete(Map<String, Object> para) {
|
|
|
+ String tableName = (String) para.get("tableName");
|
|
|
+ JSONObject condition = (JSONObject) para.get("condition");
|
|
|
+ JSONObject packColCondition = (JSONObject) para.get("packColCondition");
|
|
|
+
|
|
|
+ StringBuilder sql = new StringBuilder();
|
|
|
+ sql.append("DELETE FROM ");
|
|
|
+ sql.append(tableName);
|
|
|
+ packCondition(packColCondition, sql);
|
|
|
+
|
|
|
+ String sqlStr = sql.toString();
|
|
|
+ LOGGER.info("通用删除的sql语句为: {} \r\n condition: {}", sqlStr, condition);
|
|
|
+ return sqlStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String deleteByIdList(Map<String, Object> para) {
|
|
|
+ String tableName = (String) para.get("tableName");
|
|
|
+ String pkName = (String) para.get("pkName");
|
|
|
+ List<Long> idList = (List<Long>) para.get(ID_LIST);
|
|
|
+
|
|
|
+ StringBuilder wholeSql = new StringBuilder();
|
|
|
+ wholeSql.append("delete ").append(" from ").append(tableName).append(" where ").append(pkName).append(" in ( ");
|
|
|
+
|
|
|
+ for (int i = 0; i < idList.size(); ++i) {
|
|
|
+ wholeSql.append(String.format("#{idList[%d]}", i));
|
|
|
+ wholeSql.append(",");
|
|
|
+ }
|
|
|
+
|
|
|
+ wholeSql.setCharAt(wholeSql.length() - 1, ')');
|
|
|
+ String sqlStr = wholeSql.toString();
|
|
|
+ LOGGER.info("通用删除的sql语句为:{} \r\n deleteByIds idList: {}", sqlStr, idList);
|
|
|
+ return sqlStr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /* ***************************************************** update ******************************************************/
|
|
|
+
|
|
|
public String updateByIdList(Map<String, Object> para) {
|
|
|
String tableName = (String) para.get("tableName");
|
|
|
String pkName = (String) para.get("pkName");
|
|
@@ -410,7 +298,33 @@ public interface StandardlyMapper {
|
|
|
String sqlStr = wholeSql.toString();
|
|
|
LOGGER.info("批量更新的sql语句为: {} \r\n 更改的数据为: {} \r\n idList: {}", sqlStr, models, idList);
|
|
|
return sqlStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String update(Map<String, Object> para) {
|
|
|
+ String tableName = (String) para.get("tableName");
|
|
|
+ JSONObject packCommitData = (JSONObject) para.get("packCommitData");
|
|
|
+ JSONObject packColCondition = (JSONObject) para.get("packColCondition");
|
|
|
+
|
|
|
+ StringBuilder wholeSql = new StringBuilder();
|
|
|
+ wholeSql.append("update ").append(tableName).append(" set ");
|
|
|
+
|
|
|
+ for (Map.Entry<String, Object> entry : packCommitData.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Object valueObj = entry.getValue();
|
|
|
+ List<String> types = ((List<String>) valueObj);
|
|
|
+ /** {@link com.boman.web.core.utils.ColumnUtils.packColCondition} 这里是拼参数的地方 **/
|
|
|
+ Object value = types.get(0);
|
|
|
+ String queryType = types.get(1);
|
|
|
+ String columnType = types.get(2);
|
|
|
+ wholeSql.append(key).append(covert(queryType, columnType, key, value)).append(" , ");
|
|
|
+ }
|
|
|
|
|
|
+ wholeSql.append(StringUtils.substringBeforeLast(wholeSql.toString(), " , "));
|
|
|
+ packCondition(packColCondition, wholeSql);
|
|
|
+
|
|
|
+ String sqlStr = wholeSql.toString();
|
|
|
+ LOGGER.info("通用的更新sql语句为: {} \r\n 提交的数据为: {} \r\n 条件为: {}", sqlStr, packCommitData, packColCondition);
|
|
|
+ return sqlStr;
|
|
|
}
|
|
|
|
|
|
public String updateByForeignKey(Map<String, Object> para) {
|
|
@@ -442,151 +356,11 @@ public interface StandardlyMapper {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public String delete(Map<String, Object> para) {
|
|
|
- Long[] ids = (Long[]) para.get("ids");
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- String pkName = (String) para.get("pkName");
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (ids == null) {
|
|
|
- throw new IllegalArgumentException("ids 无效");
|
|
|
- } else if (ids.length == 0) {
|
|
|
- return "";
|
|
|
- } else if (ids.length == 1) {
|
|
|
- return "DELETE FROM " + tableName + " where " + pkName + " = #{ids[0]}";
|
|
|
- } else {
|
|
|
- StringBuffer sql = new StringBuffer();
|
|
|
- sql.append("DELETE FROM ");
|
|
|
- sql.append(tableName);
|
|
|
- sql.append(" WHERE ");
|
|
|
- sql.append(pkName + " in (");
|
|
|
-
|
|
|
- for (int i = 0; i < ids.length; ++i) {
|
|
|
- sql.append(String.format("#{ids[%d]}", i));
|
|
|
- sql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.setCharAt(sql.length() - 1, ')');
|
|
|
- String sqlStr = sql.toString();
|
|
|
- LOGGER.info("删除的sql语句为: {} \r\n idList: {}", sqlStr, ids);
|
|
|
- return sqlStr;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- public String updates(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- Collection<Object> models = (Collection) para.get("models");
|
|
|
- Consumer<JSONObject> processor = (Consumer) para.get("processor");
|
|
|
- Boolean first = true;
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (models != null && models.size() != 0) {
|
|
|
- JSONObject columnall = new JSONObject(true);
|
|
|
- Iterator var7 = models.iterator();
|
|
|
-
|
|
|
- while (var7.hasNext()) {
|
|
|
- Object obj = var7.next();
|
|
|
- JSONObject columnpart = (JSONObject) obj;
|
|
|
- if (columnpart == null) {
|
|
|
- columnpart = new JSONObject(true);
|
|
|
- }
|
|
|
-
|
|
|
- if (processor != null) {
|
|
|
- processor.accept(columnpart);
|
|
|
- }
|
|
|
-
|
|
|
- keyToUpper(columnpart);
|
|
|
- columnall.putAll(columnpart);
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject clonekeyobj = (JSONObject) columnall.clone();
|
|
|
- Set<String> keys = columnall.keySet();
|
|
|
- Iterator var19 = keys.iterator();
|
|
|
|
|
|
- while (var19.hasNext()) {
|
|
|
- String column = (String) var19.next();
|
|
|
- if (isReadOnly(column)) {
|
|
|
- clonekeyobj.remove(column);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Set<String> firstkeys = clonekeyobj.keySet();
|
|
|
- StringBuffer sql = new StringBuffer("");
|
|
|
- sql.append("UPDATE ").append(tableName).append(" a");
|
|
|
- sql.append(" right join (");
|
|
|
- int i = -1;
|
|
|
- Iterator var12 = models.iterator();
|
|
|
-
|
|
|
- while (true) {
|
|
|
- while (var12.hasNext()) {
|
|
|
- Object obj = var12.next();
|
|
|
- ++i;
|
|
|
- JSONObject model = (JSONObject) obj;
|
|
|
- Iterator var15;
|
|
|
- String key;
|
|
|
- if (first.booleanValue()) {
|
|
|
- sql.append(" SELECT #{models[0].ID} `ID`,");
|
|
|
- var15 = firstkeys.iterator();
|
|
|
-
|
|
|
- while (var15.hasNext()) {
|
|
|
- key = (String) var15.next();
|
|
|
- if (model.get(key) == null) {
|
|
|
- model.put(key, (Object) null);
|
|
|
- }
|
|
|
-
|
|
|
- sql.append(String.format("#{models[%d].%s} ", i, key));
|
|
|
- sql.append("`").append(key).append("`").append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- if (models.size() > 0) {
|
|
|
- sql.append(" union all");
|
|
|
- }
|
|
|
-
|
|
|
- first = false;
|
|
|
- } else {
|
|
|
- sql.append(" SELECT ");
|
|
|
- sql.append(String.format("#{models[%d].%s} ", i, "ID")).append(",");
|
|
|
- var15 = firstkeys.iterator();
|
|
|
-
|
|
|
- while (var15.hasNext()) {
|
|
|
- key = (String) var15.next();
|
|
|
- if (model.get(key) == null) {
|
|
|
- model.put(key, (Object) null);
|
|
|
- }
|
|
|
-
|
|
|
- sql.append(String.format("#{models[%d].%s} ", i, key));
|
|
|
- sql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- if (i < models.size() - 1) {
|
|
|
- sql.append(" union all");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- sql.append(") b on a.ID=b.ID ");
|
|
|
- sql.append(" SET ");
|
|
|
- var12 = firstkeys.iterator();
|
|
|
-
|
|
|
- while (var12.hasNext()) {
|
|
|
- String value = (String) var12.next();
|
|
|
- sql.append(" a.").append(value).append("=").append("IFNULL(b.").append(value).append(",").append("a.").append(value).append(")").append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- String sqlStr = sql.toString();
|
|
|
- LOGGER.info("更新的sql语句为: {}", sqlStr);
|
|
|
- return sqlStr;
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- }
|
|
|
- }
|
|
|
+ /* ***************************************************** select ******************************************************/
|
|
|
|
|
|
public String selectByCondition(Map<String, Object> para) {
|
|
|
- JSONObject condition = (JSONObject) para.get("condition");
|
|
|
JSONObject packCondition = (JSONObject) para.get("packCondition");
|
|
|
String tableName = (String) para.get("tableName");
|
|
|
String orderBy = (String) para.get("orderBy");
|
|
@@ -619,7 +393,6 @@ public interface StandardlyMapper {
|
|
|
}
|
|
|
|
|
|
public String getByMap(Map<String, Object> para) {
|
|
|
- JSONObject condition = (JSONObject) para.get("condition");
|
|
|
JSONObject packCondition = (JSONObject) para.get("packCondition");
|
|
|
String tableName = (String) para.get("tableName");
|
|
|
boolean limitOne = (boolean) para.get("limitOne");
|
|
@@ -627,7 +400,7 @@ public interface StandardlyMapper {
|
|
|
String result;
|
|
|
StringBuilder wholeSql = new StringBuilder();
|
|
|
wholeSql.append("select * from ").append(tableName);
|
|
|
- if (isEmpty(condition)) {
|
|
|
+ if (isEmpty(packCondition)) {
|
|
|
result = wholeSql.toString();
|
|
|
} else {
|
|
|
// 条件
|
|
@@ -636,7 +409,7 @@ public interface StandardlyMapper {
|
|
|
}
|
|
|
|
|
|
result = limitOne ? result + " limit 1" : result;
|
|
|
- LOGGER.info("查询拼出的sql语句为: {} \r\n getByMap 查询条件: {}", result, condition);
|
|
|
+ LOGGER.info("查询拼出的sql语句为: {} \r\n getByMap 查询条件: {}", result, packCondition);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -673,7 +446,6 @@ public interface StandardlyMapper {
|
|
|
}
|
|
|
|
|
|
public String countByCondition(Map<String, Object> para) {
|
|
|
- JSONObject condition = (JSONObject) para.get("condition");
|
|
|
JSONObject packCondition = (JSONObject) para.get("packCondition");
|
|
|
String tableName = (String) para.get("tableName");
|
|
|
|
|
@@ -692,22 +464,24 @@ public interface StandardlyMapper {
|
|
|
String tableName = (String) params.get("tableName");
|
|
|
String pkName = (String) params.get("pkName");
|
|
|
Long id = (Long) params.get("id");
|
|
|
- String other = (String)params.get("other");
|
|
|
+ String other = (String) params.get("other");
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
- if(StringUtils.isEmpty(other)) {
|
|
|
+ if (StringUtils.isEmpty(other)) {
|
|
|
sqlBuilder.append("select * from ")
|
|
|
.append(tableName).append(" where ")
|
|
|
- .append(pkName).append(" = ")
|
|
|
- .append(""+ id +"").append(" limit 1");
|
|
|
- }else if(other.equals("leave")) {
|
|
|
+ .append(pkName).append(" = ").append(id).append(" limit 1");
|
|
|
+ } else if ("leave".equals(other)) {
|
|
|
sqlBuilder.append("select t.*,u.nick_name from ")
|
|
|
.append(tableName).append(" t ").append(" JOIN sys_user u ON u.user_name = t.leavefrom_user_name ")
|
|
|
.append(" where ")
|
|
|
- .append("t.").append(pkName).append(" = ").append(""+ id +"").append(" limit 1");
|
|
|
+ .append("t.").append(pkName).append(" = ").append(id).append(" limit 1");
|
|
|
}
|
|
|
return sqlBuilder.toString();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /* ***************************************************** utils ******************************************************/
|
|
|
+
|
|
|
/**
|
|
|
* 功能描述: 判断传过来的值是否需要转义
|
|
|
*
|
|
@@ -746,7 +520,7 @@ public interface StandardlyMapper {
|
|
|
*/
|
|
|
private String covert(String queryType, String columnType, String key, Object valueObj) {
|
|
|
// false 不需要转义
|
|
|
- boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)|| columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
|
|
|
+ boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR) || columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
|
|
|
Object value;
|
|
|
switch (queryType) {
|
|
|
case EQ:
|
|
@@ -772,7 +546,46 @@ public interface StandardlyMapper {
|
|
|
default:
|
|
|
// in
|
|
|
List<Object> list = valueObj instanceof List ? ((List<Object>) valueObj) : Lists.newArrayList(valueObj);
|
|
|
- return " in (" + ColumnUtils.joinList(list) + ")";
|
|
|
+ return " in (" + ColumnUtils.joinList(list) + ")";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static final String[] READONLY_COLUMNS = new String[]{"OWNERID", "OWNERNAME", "OWNERENAME", "CREATIONDATE", "ID"};
|
|
|
+
|
|
|
+ public SqlProvider() {
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isReadOnly(String column) {
|
|
|
+ String[] var1 = READONLY_COLUMNS;
|
|
|
+ int var2 = var1.length;
|
|
|
+
|
|
|
+ for (String readonlyColumn : var1) {
|
|
|
+ if (readonlyColumn.equals(column)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isNullOrEmpty(String str) {
|
|
|
+ return str == null || str.length() == 0 || str.trim().length() == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isNullOrEmpty(JSONObject json) {
|
|
|
+ return json == null || json.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void keyToUpper(JSONObject json) {
|
|
|
+ String[] keys = new String[json.size()];
|
|
|
+ json.keySet().toArray(keys);
|
|
|
+ int var3 = keys.length;
|
|
|
+
|
|
|
+ for (String key : keys) {
|
|
|
+ if (!key.equals(key.toUpperCase())) {
|
|
|
+ json.put(key.toUpperCase(), json.get(key));
|
|
|
+ json.remove(key);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|