From 8b8ab744838afac3f8d93712cf2dcba075d329ae Mon Sep 17 00:00:00 2001 From: "youhong.ai" Date: Fri, 12 May 2023 21:12:06 +0800 Subject: [PATCH] =?UTF-8?q?mapper=E6=8B=BC=E6=8E=A5=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/recordset/MapperBuilderSql.java | 110 ++++++++++++ .../builderSql/impl/BuilderSqlImpl.java | 157 +++++++++--------- .../hrmNew/JobTitleMultilingualUtil.java | 10 +- 3 files changed, 202 insertions(+), 75 deletions(-) create mode 100644 src/main/java/aiyh/utils/recordset/MapperBuilderSql.java diff --git a/src/main/java/aiyh/utils/recordset/MapperBuilderSql.java b/src/main/java/aiyh/utils/recordset/MapperBuilderSql.java new file mode 100644 index 0000000..13fff23 --- /dev/null +++ b/src/main/java/aiyh/utils/recordset/MapperBuilderSql.java @@ -0,0 +1,110 @@ +package aiyh.utils.recordset; + +import aiyh.utils.excention.CustomerException; +import aiyh.utils.tool.cn.hutool.core.collection.CollectionUtil; +import aiyh.utils.tool.cn.hutool.core.util.StrUtil; + +import java.util.Map; + +/** + *

构建sql

+ * + *

create: 2023/5/12 20:51

+ * + * @author youHong.ai + */ +public class MapperBuilderSql { + + /** + *

构建更新sql

+ * + * @param table 表名称 + * @param param 参数 + * @return 构建的sql + */ + public static String builderUpdateSql(String table, Map param) { + if (StrUtil.isBlank(table) || CollectionUtil.isEmpty(param)) { + throw new CustomerException("tableName or param can not to be null!"); + } + StringBuilder sb = new StringBuilder(); + sb.append("update ").append(table).append(" set "); + for (Map.Entry entry : param.entrySet()) { + sb.append(" ") + .append(entry.getKey()) + .append(" = ") + .append("#{") + .append(entry.getKey()) + .append("}") + .append(","); + } + sb.deleteCharAt(sb.length() - 1); + return sb.toString(); + } + + /** + *

构建插入sql

+ * + * @param table 表名称 + * @param param 参数 + * @return 构建的sql + */ + public static String builderInsertSql(String table, Map param) { + if (StrUtil.isBlank(table) || CollectionUtil.isEmpty(param)) { + throw new CustomerException("tableName or param can not to be null!"); + } + StringBuilder sb = new StringBuilder(); + StringBuilder sbValue = new StringBuilder(); + sb.append("insert into ").append(table).append(" ("); + sbValue.append(") values ( "); + for (Map.Entry entry : param.entrySet()) { + sb.append(entry.getKey()).append(" ,"); + sbValue.append(" #{").append(entry.getKey()).append("},"); + } + sb.deleteCharAt(sb.length() - 1); + sbValue.deleteCharAt(sbValue.length() - 1); + sb.append(sbValue).append(")"); + return sb.toString(); + } + + public static String builderWhereAnd(Map param, boolean containsWhere) { + return builderWhereAnd(param, "whereParam", containsWhere); + } + + public static String builderWhereAnd(Map param) { + return builderWhereAnd(param, "whereParam", true); + } + + public static String builderNoWhereAndEn(Map param, boolean containsWhere) { + return builderWhereAnd(param, "", containsWhere); + } + + public static String builderNoWhereAndEn(Map param) { + return builderWhereAnd(param, "", false); + } + + /** + *

构建and条件

+ * + * @param param 参数 + * @param wherePrefix 条件拼接前缀 + * @param containsWhere 是否包含where + * @return 构建的where条件 + */ + public static String builderWhereAnd(Map param, String wherePrefix, boolean containsWhere) { + if (CollectionUtil.isEmpty(param)) { + throw new CustomerException("tableName or param can not to be null!"); + } + StringBuilder sb = new StringBuilder(); + if (containsWhere) { + sb.append(" where "); + } + for (Map.Entry entry : param.entrySet()) { + sb.append(entry.getKey()).append(" and ").append("#{"); + if (StrUtil.isNotBlank(wherePrefix)) { + sb.append(wherePrefix).append("."); + } + sb.append(entry.getKey()).append("}"); + } + return sb.toString(); + } +} diff --git a/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java b/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java index 8cf0589..82f14a0 100644 --- a/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java +++ b/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java @@ -5,18 +5,15 @@ import aiyh.utils.Util; import aiyh.utils.mapUtil.UtilHashMap; import aiyh.utils.mapUtil.UtilLinkedHashMap; import aiyh.utils.sqlUtil.builderSql.BuilderSql; -import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl; import aiyh.utils.sqlUtil.sqlResult.impl.BatchSqlResultImpl; +import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl; import aiyh.utils.sqlUtil.whereUtil.Where; import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl; import weaver.conn.RecordSet; - import java.beans.BeanInfo; -import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -29,15 +26,16 @@ import java.util.stream.Collectors; public class BuilderSqlImpl implements BuilderSql { - private String DB_TYPE; - + private final String DB_TYPE; + { // 获取当前数据库的类型 this.DB_TYPE = (new RecordSet()).getDBType(); } - + /** * 构建插入语句 + * * @param tableName 数据库表名 * @param mapConfig 数据库字段和值 * @return 自定义SQL实体类 @@ -63,21 +61,22 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(" )"); return new PrepSqlResultImpl(sqlBuilder.toString(), args); } - - /** - * 通过实体类构建插入SQL - * @param tableName 数据库表名 - * @param t 实体类对象 - * @param 实体类对象泛型 - * @return SQL结果对象 - */ - public PrepSqlResultImpl insertSqlByEntity(String tableName, T t){ + + /** + * 通过实体类构建插入SQL + * + * @param tableName 数据库表名 + * @param t 实体类对象 + * @param 实体类对象泛型 + * @return SQL结果对象 + */ + public PrepSqlResultImpl insertSqlByEntity(String tableName, T t) { List args = new ArrayList<>(); StringBuilder sqlBuilder = new StringBuilder("insert into "); sqlBuilder.append(tableName); StringBuilder fieldBuilder = new StringBuilder(); StringBuilder valueBuilder = new StringBuilder(); - + BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(t.getClass(), Object.class); @@ -88,7 +87,7 @@ public class BuilderSqlImpl implements BuilderSql { Object invoke = readMethod.invoke(t); // System.out.println(name); // System.out.println(invoke); - if(invoke == null){ + if (invoke == null) { continue; } fieldBuilder.append(name); @@ -106,10 +105,11 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(" )"); return new PrepSqlResultImpl(sqlBuilder.toString(), args); } - + /** * 构建批量插入SQL - * @param tableName 表名 + * + * @param tableName 表名 * @param mapListConfig 表对应的字段和值映射list * @return 自定义批量SQL实体类 */ @@ -142,14 +142,15 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(" )"); return new BatchSqlResultImpl(sqlBuilder.toString(), args); } - - /** - * 构建批量插入SQL - * @param tableName 数据库表名 - * @param list 实体类数组 - * @param 实体类泛型 - * @return SQL对象 - */ + + /** + * 构建批量插入SQL + * + * @param tableName 数据库表名 + * @param list 实体类数组 + * @param 实体类泛型 + * @return SQL对象 + */ public BatchSqlResultImpl insertBatchSqlByEntity(String tableName, List list) { StringBuilder sqlBuilder = new StringBuilder("insert into "); sqlBuilder.append(tableName); @@ -157,11 +158,11 @@ public class BuilderSqlImpl implements BuilderSql { StringBuilder valueBuilder = new StringBuilder(); List args = new ArrayList<>(); AtomicInteger i = new AtomicInteger(); - + list.forEach(item -> { List arg = new ArrayList<>(); BeanInfo beanInfo = null; - + try { beanInfo = Introspector.getBeanInfo(item.getClass(), Object.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); @@ -169,7 +170,7 @@ public class BuilderSqlImpl implements BuilderSql { String name = proper.getName(); Method readMethod = proper.getReadMethod(); Object invoke = readMethod.invoke(item); - if(invoke == null){ + if (invoke == null) { continue; } if (i.get() == 0) { @@ -192,12 +193,13 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(" )"); return new BatchSqlResultImpl(sqlBuilder.toString(), args); } - + /** - * 构建更新语句 + * 构建更新语句 + * * @param tableName 表名 * @param mapConfig 表名所对应键值对 - * @param where 更新数据的条件 + * @param where 更新数据的条件 * @return 自定义SQL实体类 */ @Override @@ -222,16 +224,17 @@ public class BuilderSqlImpl implements BuilderSql { } return new PrepSqlResultImpl(sqlBuilder.toString(), args); } - - /** - * 构建更新SQL语句 - * @param tableName 数据库表名 - * @param t 实体类对象 - * @param where 更新条件对象 - * @param 实体类泛型 - * @return 构建后的SQL对象 - */ - public PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where){ + + /** + * 构建更新SQL语句 + * + * @param tableName 数据库表名 + * @param t 实体类对象 + * @param where 更新条件对象 + * @param 实体类泛型 + * @return 构建后的SQL对象 + */ + public PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where) { this.verifyWhere(where); StringBuilder sqlBuilder = new StringBuilder("update "); StringBuilder fieldValue = new StringBuilder(); @@ -246,7 +249,7 @@ public class BuilderSqlImpl implements BuilderSql { String name = proper.getName(); Method readMethod = proper.getReadMethod(); Object invoke = readMethod.invoke(t); - if(invoke == null){ + if (invoke == null) { continue; } fieldValue.append(name); @@ -264,12 +267,12 @@ public class BuilderSqlImpl implements BuilderSql { } return new PrepSqlResultImpl(sqlBuilder.toString(), args); } - - + + /** - * @param tableName 表名 + * @param tableName 表名 * @param mapListConfig 表名所对应的键值对数组 - * @param whereList 条件数组 + * @param whereList 条件数组 * @return 批量更新只适用于更新条件一样,但是参数不同的时候,比如更新条件都是id= ?,但是各id的数值不一样 */ @Override @@ -305,15 +308,16 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(whereList.get(0).getSql()); return new BatchSqlResultImpl(sqlBuilder.toString(), args); } - - /** - * 通过实体类构建批量更新SQL - * @param tableName 数据库表名 - * @param list 实体类集合 - * @param whereList 更新条件集合,一一对应更新数据集合 - * @param 泛型 - * @return 构建后的批量SQL对象 - */ + + /** + * 通过实体类构建批量更新SQL + * + * @param tableName 数据库表名 + * @param list 实体类集合 + * @param whereList 更新条件集合,一一对应更新数据集合 + * @param 泛型 + * @return 构建后的批量SQL对象 + */ public BatchSqlResultImpl updateBatchSqlByEntity(String tableName, List list, List whereList) { this.verifyWhereList(whereList); if (list.size() != whereList.size()) { @@ -335,7 +339,7 @@ public class BuilderSqlImpl implements BuilderSql { String name = proper.getName(); Method readMethod = proper.getReadMethod(); Object invoke = readMethod.invoke(item); - if(invoke == null){ + if (invoke == null) { continue; } if (i.get() == 0) { @@ -358,9 +362,10 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(whereList.get(0).getSql()); return new BatchSqlResultImpl(sqlBuilder.toString(), args); } - + /** * 构建插入SQL语句 + * * @param tableName 表名 * @param mapConfig 参数 * @return 拼接好的SQL @@ -387,9 +392,10 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(" )"); return sqlBuilder.toString(); } - + /** * 构建更新SQL语句 + * * @param tableName 表名 * @param mapConfig 参数 * @return 拼接好的SQL @@ -415,27 +421,27 @@ public class BuilderSqlImpl implements BuilderSql { sqlBuilder.append(where.getSql()); return sqlBuilder.toString(); } - - - - + + /** * 验证构建SQL参数的,并且过滤到为null的数据 + * * @param map 验证SQL参数map对象 * @return 验证后的UtilHashMap */ public UtilHashMap verifyMap(Map map) { UtilHashMap filterMap = Util.createUtilHashMap() - .uPutAll(map) - .filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value)); + .uPutAll(map) + .filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value)); if (Util.mapIsNullOrEmpty(filterMap)) { throw new RuntimeException("map为空或没有数据! map is null or empty!"); } return filterMap; } - + /** * 验证批量SQL构建的数据 + * * @param mapList 批量构建SQL的的工具 * @return 验证和过滤后的数据 */ @@ -444,18 +450,19 @@ public class BuilderSqlImpl implements BuilderSql { throw new RuntimeException("mapList为null!mapList is null!"); } List> collect = mapList.stream().map(item -> - Util.createUtilLinkedHashMap() - .uPutAll(item) - .filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value)) + Util.createUtilLinkedHashMap() + .uPutAll(item) + .filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value)) ).collect(Collectors.toList()); if (mapList.size() == 0) { throw new RuntimeException("mapList没有数据!mapList is empty!"); } return collect; } - + /** * 验证where条件是否符合 + * * @param where where 条件对象 */ private void verifyWhere(Where where) { @@ -463,16 +470,18 @@ public class BuilderSqlImpl implements BuilderSql { throw new RuntimeException("where为null! where is null!"); } } + /** * 验证where条件集合是否符合 + * * @param whereList where 条件对象集合 */ private void verifyWhereList(List whereList) { if (whereList == null) { throw new RuntimeException("whereList为null! whereList is null!"); } - whereList.forEach(item->{ - if(item == null){ + whereList.forEach(item -> { + if (item == null) { throw new RuntimeException("whereList中数据为null! whereDate is null in whereList!"); } }); diff --git a/src/main/youhong_ai_jitu_src/jntchina/schedule/hrmNew/JobTitleMultilingualUtil.java b/src/main/youhong_ai_jitu_src/jntchina/schedule/hrmNew/JobTitleMultilingualUtil.java index 552f87c..80def67 100644 --- a/src/main/youhong_ai_jitu_src/jntchina/schedule/hrmNew/JobTitleMultilingualUtil.java +++ b/src/main/youhong_ai_jitu_src/jntchina/schedule/hrmNew/JobTitleMultilingualUtil.java @@ -1,8 +1,10 @@ package jntchina.schedule.hrmNew; import aiyh.utils.Util; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import org.apache.log4j.Logger; import java.util.*; import java.util.function.Function; @@ -21,7 +23,8 @@ public class JobTitleMultilingualUtil { private JSONArray jsonArray = null; private Map> groupMap = null; - private final Long updateTime = 0L; + private Long updateTime = 0L; + private final Logger log = Util.getLogger(); static { extracted(); @@ -148,6 +151,7 @@ public class JobTitleMultilingualUtil { long l = System.currentTimeMillis(); if (l - updateTime >= 1_000 * 60 * 60 * 3) { extracted(); + updateTime = System.currentTimeMillis(); } StringBuilder sb = new StringBuilder(); List list = jsonArray.toJavaList(Map.class); @@ -156,6 +160,7 @@ public class JobTitleMultilingualUtil { if (!jsonArray.equals(this.jsonArray)) { this.jsonArray = jsonArray; this.groupMap = list.stream().collect(Collectors.groupingBy(getKey)); + log.info("job title group : " + JSON.toJSONString(this.groupMap)); } String active = LANGUAGE_MAP.get("active"); if (isBlank(active)) { @@ -165,9 +170,12 @@ public class JobTitleMultilingualUtil { active += ",ZHS"; String[] activeLanguageArray = active.split(","); List activeList = Arrays.stream(activeLanguageArray).distinct().collect(Collectors.toList()); + log.info("active language: " + JSON.toJSONString(activeList)); + log.info("language map: " + JSON.toJSONString(LANGUAGE_MAP)); groupMap = this.groupMap; if (!isEmpty(groupMap)) { List maps = groupMap.get(getKey.apply(JSONObject.toJavaObject(currentObj, Map.class))); + log.info("current job Maps: " + JSON.toJSONString(maps)); if (!isEmpty(maps)) { sb.append("~`"); for (Map map : maps) {