添加自定义注解判断是否因为数据为空时过滤当前记录

This commit is contained in:
2023-07-21 09:33:10 +08:00
parent 2b36c07bc0
commit 72ba32bc61
3 changed files with 51 additions and 10 deletions

View 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();
}

View File

@@ -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;
@@ -75,21 +76,26 @@ 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(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);
} }

View File

@@ -29,12 +29,17 @@ 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{ }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());
} }
} else {
sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", args[i].toString());
sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());
}
} }
return sql; return sql;
} }