书接上回
Java 工作日记 通过实体类名获取注解名称_o(╯□╰)o134的博客-CSDN博客
上一个是通过名字取值 现在是赋值
同样还是用的反射
实体类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Data
@ApiModel("项目信息")
public class PmsProjectInfo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("项目主键")
private String projectCode;
@ApiModelProperty("项目标题")
private String projectTitle;
@ApiModelProperty("单位编码")
private String departmentCode;
@ApiModelProperty("单位名称")
private String departmentName;
@ApiModelProperty("建设年度")
private Integer year;
@ApiModelProperty("建设地点")
private String address;
@ApiModelProperty("纬度")
private BigDecimal lat;
@ApiModelProperty("经度")
private BigDecimal lon;
@ApiModelProperty("项目类型")
private String projectType;
@ApiModelProperty("项目状态")
private ProjectStatus projectStatus;
@ApiModelProperty("项目库")
private ProjectLibrary projectLibrary;
@ApiModelProperty("二级分类")
private ProjectSubclass secondSecond;
@ApiModelProperty("所属专项")
private String special;
@ApiModelProperty("资金来源")
private String capitalSource;
@ApiModelProperty("建设主体")
private String constitutiveSubject;
@ApiModelProperty("总投资(万)")
private BigDecimal totalFund;
@ApiModelProperty("拟申请上级财政资金(万)")
private BigDecimal financialFund;
@ApiModelProperty("企业配套资金(万)")
private BigDecimal supportingFund;
@ApiModelProperty("企业自筹资金(万)")
private BigDecimal selfFund;
@ApiModelProperty("其他资金(万)")
private BigDecimal otherFund;
@ApiModelProperty("建设内容")
private String body;
@ApiModelProperty("前期工作开展情况")
private String preliminaryWork;
@ApiModelProperty("建设难点和可颠覆性因素")
private String difficulty;
@ApiModelProperty("备注")
private String remark;
@ApiModelProperty("计划开工时间")
private LocalDateTime startTime;
@ApiModelProperty("计划完工时间")
private LocalDateTime endTime;
@ApiModelProperty("实际开工时间")
private LocalDateTime actualStartTime;
@ApiModelProperty("实际完工时间")
private LocalDateTime actualEndTime;
@ApiModelProperty("负责人名称")
private String principalName;
@ApiModelProperty("负责人编码")
private String principalCode;
@ApiModelProperty("警告数")
private Integer warnCount;
@ApiModelProperty("创建时间")
private LocalDateTime createDateTime;
@ApiModelProperty("创建人编码")
private String createUserCode;
@ApiModelProperty("创建人姓名")
private String createUserName;
@ApiModelProperty("修改时间")
private LocalDateTime updateDateTime;
@ApiModelProperty("修改人编码")
private String updateUserCode;
@ApiModelProperty("修改人姓名")
private String updateUserName;
}
数据库
实现类
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModelProperty;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @description:
* @
**/
@Service
public class PmsProjectChangeServiceImpl implements PmsProjectChangeService {
private final PmsProjectChangeMapper pmsProjectChangeMapper;
private final RoleInfoService roleInfoService;
private final PmsProjectInfoMapper pmsProjectInfoMapper;
public PmsProjectChangeServiceImpl(PmsProjectChangeMapper pmsProjectChangeMapper,
RoleInfoService roleInfoService,
PmsProjectInfoMapper pmsProjectInfoMapper) {
this.pmsProjectChangeMapper = pmsProjectChangeMapper;
this.roleInfoService = roleInfoService;
this.pmsProjectInfoMapper = pmsProjectInfoMapper;
}
/**
* 变更审核
*
* @param info
* @return
*/
@Override
@Transactional
public ResponseResult examine(PmsProjectChange info) {
ResponseResult result = new ResponseResult();
PmsProjectChange projectChange = pmsProjectChangeMapper.get(info.getChangeCode());
if (Strings.isNotBlank(info.getChangeField())) {
String[] strings = info.getChangeField().split(",");
String[] newValue = info.getNewValue().split(",");
for (int s = 0; s < strings.length; s++) {
setFieldValueByName(projectInfo, strings[s], newValue[s]);
}
}
}
}
result.setStatus(true);
result.setResult(l);
return result;
}
/**
* 根据属性名给属性赋值
*
* @param obj
* @param fieldName
* @param value
*/
public static void setFieldValueByName(Object obj, String fieldName, Object value) {
try {
// 获取obj类的字节文件对象
Class c = obj.getClass();
// 获取该类的成员变量
Field f = c.getDeclaredField(fieldName);
// 取消语言访问检查
f.setAccessible(true);
// 给变量赋值
f.set(obj, converAttributeValue(f.getGenericType().toString(), value.toString()));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 转换类型防止赋值失败
*
* @param type
* @param value
* @return
*/
public static Object converAttributeValue(String type, String value) {
if ("Long".equals(type) || "class java.lang.Long".equals(type)) {
return Long.parseLong(value);
} else if ("LocalDateTime".equals(type) || "class java.time.LocalDateTime".equals(type)) {
return DateHelper.getLocalDateTime(value, "yyyy-MM-dd HH:mm:ss");
} else if ("Integer".equals(type) || "class java.lang.Integer".equals(type)) {
return Integer.parseInt(value);
} else if ("BigDecimal".equals(type) || "class java.math.BigDecimal".equals(type)) {
return new BigDecimal(value);
} else {
return value;
}
}
}
同样也是参考的