mapper拼接类

jingwei
youhong.ai 2023-05-12 21:12:06 +08:00
parent ce90fa1def
commit 8b8ab74483
3 changed files with 202 additions and 75 deletions

View File

@ -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;
/**
* <h1>sql</h1>
*
* <p>create: 2023/5/12 20:51</p>
*
* @author youHong.ai
*/
public class MapperBuilderSql {
/**
* <h2>sql</h2>
*
* @param table
* @param param
* @return sql
*/
public static String builderUpdateSql(String table, Map<String, Object> 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<String, Object> entry : param.entrySet()) {
sb.append(" ")
.append(entry.getKey())
.append(" = ")
.append("#{")
.append(entry.getKey())
.append("}")
.append(",");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* <h2>sql</h2>
*
* @param table
* @param param
* @return sql
*/
public static String builderInsertSql(String table, Map<String, Object> 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<String, Object> 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<String, Object> param, boolean containsWhere) {
return builderWhereAnd(param, "whereParam", containsWhere);
}
public static String builderWhereAnd(Map<String, Object> param) {
return builderWhereAnd(param, "whereParam", true);
}
public static String builderNoWhereAndEn(Map<String, Object> param, boolean containsWhere) {
return builderWhereAnd(param, "", containsWhere);
}
public static String builderNoWhereAndEn(Map<String, Object> param) {
return builderWhereAnd(param, "", false);
}
/**
* <h2>and</h2>
*
* @param param
* @param wherePrefix
* @param containsWhere where
* @return where
*/
public static String builderWhereAnd(Map<String, Object> 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<String, Object> 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();
}
}

View File

@ -5,18 +5,15 @@ import aiyh.utils.Util;
import aiyh.utils.mapUtil.UtilHashMap; import aiyh.utils.mapUtil.UtilHashMap;
import aiyh.utils.mapUtil.UtilLinkedHashMap; import aiyh.utils.mapUtil.UtilLinkedHashMap;
import aiyh.utils.sqlUtil.builderSql.BuilderSql; 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.BatchSqlResultImpl;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.sqlUtil.whereUtil.Where; import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl; import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl;
import weaver.conn.RecordSet; import weaver.conn.RecordSet;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector; import java.beans.Introspector;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -29,15 +26,16 @@ import java.util.stream.Collectors;
public class BuilderSqlImpl implements BuilderSql { public class BuilderSqlImpl implements BuilderSql {
private String DB_TYPE; private final String DB_TYPE;
{ {
// 获取当前数据库的类型 // 获取当前数据库的类型
this.DB_TYPE = (new RecordSet()).getDBType(); this.DB_TYPE = (new RecordSet()).getDBType();
} }
/** /**
* *
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @return SQL * @return SQL
@ -63,21 +61,22 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(" )"); sqlBuilder.append(" )");
return new PrepSqlResultImpl(sqlBuilder.toString(), args); return new PrepSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* SQL * SQL
* @param tableName *
* @param t * @param tableName
* @param <T> * @param t
* @return SQL * @param <T>
*/ * @return SQL
public <T> PrepSqlResultImpl insertSqlByEntity(String tableName, T t){ */
public <T> PrepSqlResultImpl insertSqlByEntity(String tableName, T t) {
List<Object> args = new ArrayList<>(); List<Object> args = new ArrayList<>();
StringBuilder sqlBuilder = new StringBuilder("insert into "); StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName); sqlBuilder.append(tableName);
StringBuilder fieldBuilder = new StringBuilder(); StringBuilder fieldBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder(); StringBuilder valueBuilder = new StringBuilder();
BeanInfo beanInfo = null; BeanInfo beanInfo = null;
try { try {
beanInfo = Introspector.getBeanInfo(t.getClass(), Object.class); beanInfo = Introspector.getBeanInfo(t.getClass(), Object.class);
@ -88,7 +87,7 @@ public class BuilderSqlImpl implements BuilderSql {
Object invoke = readMethod.invoke(t); Object invoke = readMethod.invoke(t);
// System.out.println(name); // System.out.println(name);
// System.out.println(invoke); // System.out.println(invoke);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
fieldBuilder.append(name); fieldBuilder.append(name);
@ -106,10 +105,11 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(" )"); sqlBuilder.append(" )");
return new PrepSqlResultImpl(sqlBuilder.toString(), args); return new PrepSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* SQL * SQL
* @param tableName *
* @param tableName
* @param mapListConfig list * @param mapListConfig list
* @return SQL * @return SQL
*/ */
@ -142,14 +142,15 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(" )"); sqlBuilder.append(" )");
return new BatchSqlResultImpl(sqlBuilder.toString(), args); return new BatchSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* SQL * SQL
* @param tableName *
* @param list * @param tableName
* @param <T> * @param list
* @return SQL * @param <T>
*/ * @return SQL
*/
public <T> BatchSqlResultImpl insertBatchSqlByEntity(String tableName, List<T> list) { public <T> BatchSqlResultImpl insertBatchSqlByEntity(String tableName, List<T> list) {
StringBuilder sqlBuilder = new StringBuilder("insert into "); StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName); sqlBuilder.append(tableName);
@ -157,11 +158,11 @@ public class BuilderSqlImpl implements BuilderSql {
StringBuilder valueBuilder = new StringBuilder(); StringBuilder valueBuilder = new StringBuilder();
List<List> args = new ArrayList<>(); List<List> args = new ArrayList<>();
AtomicInteger i = new AtomicInteger(); AtomicInteger i = new AtomicInteger();
list.forEach(item -> { list.forEach(item -> {
List<Object> arg = new ArrayList<>(); List<Object> arg = new ArrayList<>();
BeanInfo beanInfo = null; BeanInfo beanInfo = null;
try { try {
beanInfo = Introspector.getBeanInfo(item.getClass(), Object.class); beanInfo = Introspector.getBeanInfo(item.getClass(), Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
@ -169,7 +170,7 @@ public class BuilderSqlImpl implements BuilderSql {
String name = proper.getName(); String name = proper.getName();
Method readMethod = proper.getReadMethod(); Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(item); Object invoke = readMethod.invoke(item);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
if (i.get() == 0) { if (i.get() == 0) {
@ -192,12 +193,13 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(" )"); sqlBuilder.append(" )");
return new BatchSqlResultImpl(sqlBuilder.toString(), args); return new BatchSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* *
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @param where * @param where
* @return SQL * @return SQL
*/ */
@Override @Override
@ -222,16 +224,17 @@ public class BuilderSqlImpl implements BuilderSql {
} }
return new PrepSqlResultImpl(sqlBuilder.toString(), args); return new PrepSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* SQL * SQL
* @param tableName *
* @param t * @param tableName
* @param where * @param t
* @param <T> * @param where
* @return SQL * @param <T>
*/ * @return SQL
public <T> PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where){ */
public <T> PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where) {
this.verifyWhere(where); this.verifyWhere(where);
StringBuilder sqlBuilder = new StringBuilder("update "); StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder(); StringBuilder fieldValue = new StringBuilder();
@ -246,7 +249,7 @@ public class BuilderSqlImpl implements BuilderSql {
String name = proper.getName(); String name = proper.getName();
Method readMethod = proper.getReadMethod(); Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(t); Object invoke = readMethod.invoke(t);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
fieldValue.append(name); fieldValue.append(name);
@ -264,12 +267,12 @@ public class BuilderSqlImpl implements BuilderSql {
} }
return new PrepSqlResultImpl(sqlBuilder.toString(), args); return new PrepSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* @param tableName * @param tableName
* @param mapListConfig * @param mapListConfig
* @param whereList * @param whereList
* @return id= ?,id * @return id= ?,id
*/ */
@Override @Override
@ -305,15 +308,16 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(whereList.get(0).getSql()); sqlBuilder.append(whereList.get(0).getSql());
return new BatchSqlResultImpl(sqlBuilder.toString(), args); return new BatchSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* SQL * SQL
* @param tableName *
* @param list * @param tableName
* @param whereList * @param list
* @param <T> * @param whereList
* @return SQL * @param <T>
*/ * @return SQL
*/
public <T> BatchSqlResultImpl updateBatchSqlByEntity(String tableName, List<T> list, List<Where> whereList) { public <T> BatchSqlResultImpl updateBatchSqlByEntity(String tableName, List<T> list, List<Where> whereList) {
this.verifyWhereList(whereList); this.verifyWhereList(whereList);
if (list.size() != whereList.size()) { if (list.size() != whereList.size()) {
@ -335,7 +339,7 @@ public class BuilderSqlImpl implements BuilderSql {
String name = proper.getName(); String name = proper.getName();
Method readMethod = proper.getReadMethod(); Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(item); Object invoke = readMethod.invoke(item);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
if (i.get() == 0) { if (i.get() == 0) {
@ -358,9 +362,10 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(whereList.get(0).getSql()); sqlBuilder.append(whereList.get(0).getSql());
return new BatchSqlResultImpl(sqlBuilder.toString(), args); return new BatchSqlResultImpl(sqlBuilder.toString(), args);
} }
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @return SQL * @return SQL
@ -387,9 +392,10 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(" )"); sqlBuilder.append(" )");
return sqlBuilder.toString(); return sqlBuilder.toString();
} }
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @return SQL * @return SQL
@ -415,27 +421,27 @@ public class BuilderSqlImpl implements BuilderSql {
sqlBuilder.append(where.getSql()); sqlBuilder.append(where.getSql());
return sqlBuilder.toString(); return sqlBuilder.toString();
} }
/** /**
* SQLnull * SQLnull
*
* @param map SQLmap * @param map SQLmap
* @return UtilHashMap * @return UtilHashMap
*/ */
public UtilHashMap<String, Object> verifyMap(Map<String, Object> map) { public UtilHashMap<String, Object> verifyMap(Map<String, Object> map) {
UtilHashMap<String, Object> filterMap = Util.createUtilHashMap() UtilHashMap<String, Object> filterMap = Util.createUtilHashMap()
.uPutAll(map) .uPutAll(map)
.filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value)); .filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value));
if (Util.mapIsNullOrEmpty(filterMap)) { if (Util.mapIsNullOrEmpty(filterMap)) {
throw new RuntimeException("map为空或没有数据 map is null or empty!"); throw new RuntimeException("map为空或没有数据 map is null or empty!");
} }
return filterMap; return filterMap;
} }
/** /**
* SQL * SQL
*
* @param mapList SQL * @param mapList SQL
* @return * @return
*/ */
@ -444,18 +450,19 @@ public class BuilderSqlImpl implements BuilderSql {
throw new RuntimeException("mapList为nullmapList is null!"); throw new RuntimeException("mapList为nullmapList is null!");
} }
List<UtilLinkedHashMap<String, Object>> collect = mapList.stream().map(item -> List<UtilLinkedHashMap<String, Object>> collect = mapList.stream().map(item ->
Util.createUtilLinkedHashMap() Util.createUtilLinkedHashMap()
.uPutAll(item) .uPutAll(item)
.filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value)) .filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value))
).collect(Collectors.toList()); ).collect(Collectors.toList());
if (mapList.size() == 0) { if (mapList.size() == 0) {
throw new RuntimeException("mapList没有数据mapList is empty!"); throw new RuntimeException("mapList没有数据mapList is empty!");
} }
return collect; return collect;
} }
/** /**
* where * where
*
* @param where where * @param where where
*/ */
private void verifyWhere(Where where) { private void verifyWhere(Where where) {
@ -463,16 +470,18 @@ public class BuilderSqlImpl implements BuilderSql {
throw new RuntimeException("where为null where is null!"); throw new RuntimeException("where为null where is null!");
} }
} }
/** /**
* where * where
*
* @param whereList where * @param whereList where
*/ */
private void verifyWhereList(List<Where> whereList) { private void verifyWhereList(List<Where> whereList) {
if (whereList == null) { if (whereList == null) {
throw new RuntimeException("whereList为null whereList is null!"); throw new RuntimeException("whereList为null whereList is null!");
} }
whereList.forEach(item->{ whereList.forEach(item -> {
if(item == null){ if (item == null) {
throw new RuntimeException("whereList中数据为null whereDate is null in whereList!"); throw new RuntimeException("whereList中数据为null whereDate is null in whereList!");
} }
}); });

View File

@ -1,8 +1,10 @@
package jntchina.schedule.hrmNew; package jntchina.schedule.hrmNew;
import aiyh.utils.Util; import aiyh.utils.Util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
@ -21,7 +23,8 @@ public class JobTitleMultilingualUtil {
private JSONArray jsonArray = null; private JSONArray jsonArray = null;
private Map<String, List<Map>> groupMap = null; private Map<String, List<Map>> groupMap = null;
private final Long updateTime = 0L; private Long updateTime = 0L;
private final Logger log = Util.getLogger();
static { static {
extracted(); extracted();
@ -148,6 +151,7 @@ public class JobTitleMultilingualUtil {
long l = System.currentTimeMillis(); long l = System.currentTimeMillis();
if (l - updateTime >= 1_000 * 60 * 60 * 3) { if (l - updateTime >= 1_000 * 60 * 60 * 3) {
extracted(); extracted();
updateTime = System.currentTimeMillis();
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
List<Map> list = jsonArray.toJavaList(Map.class); List<Map> list = jsonArray.toJavaList(Map.class);
@ -156,6 +160,7 @@ public class JobTitleMultilingualUtil {
if (!jsonArray.equals(this.jsonArray)) { if (!jsonArray.equals(this.jsonArray)) {
this.jsonArray = jsonArray; this.jsonArray = jsonArray;
this.groupMap = list.stream().collect(Collectors.groupingBy(getKey)); this.groupMap = list.stream().collect(Collectors.groupingBy(getKey));
log.info("job title group : " + JSON.toJSONString(this.groupMap));
} }
String active = LANGUAGE_MAP.get("active"); String active = LANGUAGE_MAP.get("active");
if (isBlank(active)) { if (isBlank(active)) {
@ -165,9 +170,12 @@ public class JobTitleMultilingualUtil {
active += ",ZHS"; active += ",ZHS";
String[] activeLanguageArray = active.split(","); String[] activeLanguageArray = active.split(",");
List<String> activeList = Arrays.stream(activeLanguageArray).distinct().collect(Collectors.toList()); List<String> 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; groupMap = this.groupMap;
if (!isEmpty(groupMap)) { if (!isEmpty(groupMap)) {
List<Map> maps = groupMap.get(getKey.apply(JSONObject.toJavaObject(currentObj, Map.class))); List<Map> maps = groupMap.get(getKey.apply(JSONObject.toJavaObject(currentObj, Map.class)));
log.info("current job Maps: " + JSON.toJSONString(maps));
if (!isEmpty(maps)) { if (!isEmpty(maps)) {
sb.append("~`"); sb.append("~`");
for (Map map : maps) { for (Map map : maps) {