添加自定义注解判断是否因为数据为空时过滤当前记录
This commit is contained in:
22
src/main/java/com/njcn/influx/ano/IgnoreData.java
Normal file
22
src/main/java/com/njcn/influx/ano/IgnoreData.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.njcn.influx.ano;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断为空时,是否取消存储该条记录
|
||||||
|
* @author hongawen
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface IgnoreData {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于传参
|
||||||
|
* @return boolean 参数内容
|
||||||
|
*/
|
||||||
|
boolean value();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.njcn.influx.core;
|
|||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.text.StrPool;
|
import cn.hutool.core.text.StrPool;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.njcn.influx.ano.IgnoreData;
|
||||||
import com.njcn.influx.constant.InfluxDbSqlConstant;
|
import com.njcn.influx.constant.InfluxDbSqlConstant;
|
||||||
import com.njcn.influx.query.InfluxQueryWrapper;
|
import com.njcn.influx.query.InfluxQueryWrapper;
|
||||||
import com.njcn.influx.utils.InstantUtil;
|
import com.njcn.influx.utils.InstantUtil;
|
||||||
@@ -52,7 +53,7 @@ public class InfluxExecutor {
|
|||||||
List<Field> fields = new ArrayList<>(Arrays.asList(declaredFields));
|
List<Field> fields = new ArrayList<>(Arrays.asList(declaredFields));
|
||||||
//映射父级内部私有属性
|
//映射父级内部私有属性
|
||||||
Class superclass = domainClass.getSuperclass();
|
Class superclass = domainClass.getSuperclass();
|
||||||
if(ObjectUtil.isNotNull(superclass)){
|
if (ObjectUtil.isNotNull(superclass)) {
|
||||||
Field[] superDeclaredFields = superclass.getDeclaredFields();
|
Field[] superDeclaredFields = superclass.getDeclaredFields();
|
||||||
List<Field> superFields = new ArrayList<>(Arrays.asList(superDeclaredFields));
|
List<Field> superFields = new ArrayList<>(Arrays.asList(superDeclaredFields));
|
||||||
fields.addAll(superFields);
|
fields.addAll(superFields);
|
||||||
@@ -68,28 +69,33 @@ public class InfluxExecutor {
|
|||||||
for (QueryResult.Series series : seriesList) {
|
for (QueryResult.Series series : seriesList) {
|
||||||
obj = domainClass.newInstance();
|
obj = domainClass.newInstance();
|
||||||
Map<String, String> tags = series.getTags();
|
Map<String, String> tags = series.getTags();
|
||||||
if(CollectionUtil.isNotEmpty(tags)){
|
if (CollectionUtil.isNotEmpty(tags)) {
|
||||||
//处理tag分组数据
|
//处理tag分组数据
|
||||||
for (String columnName : tags.keySet()) {
|
for (String columnName : tags.keySet()) {
|
||||||
for (Field declaredField : fields) {
|
for (Field declaredField : fields) {
|
||||||
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
||||||
//获取属性定义的类型
|
//获取属性定义的类型
|
||||||
declaredField.setAccessible(true);
|
declaredField.setAccessible(true);
|
||||||
|
//判断是否过滤该条记录,当数据返回 null时
|
||||||
|
IgnoreData ignoreAnnotation = declaredField.getAnnotation(IgnoreData.class);
|
||||||
|
if (Objects.nonNull(ignoreAnnotation) && ignoreAnnotation.value() && Objects.isNull(tags.get(columnName))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
//时间格式
|
//时间格式
|
||||||
if (declaredField.getType() == Instant.class) {
|
if (declaredField.getType() == Instant.class) {
|
||||||
declaredField.set(obj, InstantUtil.stringToInstant(tags.get(columnName).toString().replace("+08:00", "Z")));
|
declaredField.set(obj, InstantUtil.stringToInstant(tags.get(columnName).replace("+08:00", "Z")));
|
||||||
//字符串
|
//字符串
|
||||||
} else if (declaredField.getType() == String.class) {
|
} else if (declaredField.getType() == String.class) {
|
||||||
declaredField.set(obj, tags.get(columnName).toString());
|
declaredField.set(obj, tags.get(columnName));
|
||||||
//浮点双精度
|
//浮点双精度
|
||||||
} else if (declaredField.getType() == Double.class) {
|
} else if (declaredField.getType() == Double.class) {
|
||||||
declaredField.set(obj, Double.parseDouble(tags.get(columnName).toString()));
|
declaredField.set(obj, Double.parseDouble(tags.get(columnName)));
|
||||||
//浮点
|
//浮点
|
||||||
} else if (declaredField.getType() == Float.class) {
|
} else if (declaredField.getType() == Float.class) {
|
||||||
declaredField.set(obj, Float.parseFloat(tags.get(columnName).toString()));
|
declaredField.set(obj, Float.parseFloat(tags.get(columnName)));
|
||||||
//整型
|
//整型
|
||||||
} else if (declaredField.getType() == Integer.class) {
|
} else if (declaredField.getType() == Integer.class) {
|
||||||
declaredField.set(obj, Integer.parseInt(tags.get(columnName).toString()));
|
declaredField.set(obj, Integer.parseInt(tags.get(columnName)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,6 +113,12 @@ public class InfluxExecutor {
|
|||||||
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
||||||
//获取属性定义的类型
|
//获取属性定义的类型
|
||||||
declaredField.setAccessible(true);
|
declaredField.setAccessible(true);
|
||||||
|
//判断是否过滤该条记录,当数据返回 null时
|
||||||
|
IgnoreData ignoreAnnotation = declaredField.getAnnotation(IgnoreData.class);
|
||||||
|
if (Objects.nonNull(ignoreAnnotation) && ignoreAnnotation.value() && Objects.isNull(columnValue.get(i))) {
|
||||||
|
object = null;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
//时间格式
|
//时间格式
|
||||||
if (declaredField.getType() == Instant.class) {
|
if (declaredField.getType() == Instant.class) {
|
||||||
declaredField.set(object, InstantUtil.stringToInstant(columnValue.get(i).toString().replace("+08:00", "Z")));
|
declaredField.set(object, InstantUtil.stringToInstant(columnValue.get(i).toString().replace("+08:00", "Z")));
|
||||||
@@ -126,6 +138,7 @@ public class InfluxExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (Objects.nonNull(object)) {
|
||||||
results.add((E) object);
|
results.add((E) object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,6 +146,7 @@ public class InfluxExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("influx反射实例化实体异常:" + e);
|
throw new RuntimeException("influx反射实例化实体异常:" + e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,8 +29,13 @@ public class ParameterHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (parameterType == String.class) {
|
if (parameterType == String.class) {
|
||||||
|
if (!args[i].toString().contains("select") && !args[i].toString().contains("SELECT")) {
|
||||||
sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", "'" + args[i] + "'");
|
sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", "'" + args[i] + "'");
|
||||||
sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());
|
sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());
|
||||||
|
}else{
|
||||||
|
sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", args[i].toString());
|
||||||
|
sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", args[i].toString());
|
sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", args[i].toString());
|
||||||
sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());
|
sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user