1、实体类使用@TableId/@TableField注解标记字段是否为数据库字段,且对应的数据库字段名(可以使用默认驼峰规则)
@ApiModelProperty(value = "园区编码")
@TableField("zone_code")
private String zoneCode;
@ApiModelProperty(value = "车次号")
@TableField("assign_code")
private String assignCode;
2、通过注解字段,当注解字段有值时作为查询QueryWrapper条件
@Override
public List<VehicleParkPassRec> selectList(VehicleParkPassRec vehicleParkPassRec) {
QueryWrapper<VehicleParkPassRec> queryWrapper = new QueryWrapper<>();
if (Objects.nonNull(vehicleParkPassRec)) {
Field[] fields = VehicleParkPassRec.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
Object value = field.get(vehicleParkPassRec);
if (value != null) {
TableField tableFieldAnnotation = field.getAnnotation(TableField.class);
TableId tableIdAnnotation = field.getAnnotation(TableId.class);
String columnName;
if(tableIdAnnotation != null && !tableIdAnnotation.value().isEmpty()){
columnName = tableIdAnnotation.value(); // 使用@TableId注解的值作为列名
queryWrapper.eq(columnName, value);
} else if (tableFieldAnnotation != null && tableFieldAnnotation.exist() && !tableFieldAnnotation.value().isEmpty()) {
columnName = tableFieldAnnotation.value(); // 使用@TableField注解的值作为列名
queryWrapper.eq(columnName, value);
}
}
} catch (IllegalAccessException e) {
log.error("查询车辆进出园区通行记录表数据异常: {}", e.getMessage());
return null;
}
}
} else{
// 没有指定查询条件 查询前1000条数据
queryWrapper.last("LIMIT 1000");
}
return this.list(queryWrapper);
}