验证类
public class BeanValidUtil {
/**
* 验证参数bean(包括内部的子bean)
*
* @param t
* 参数对象 例如Fxfa
* @param optype
* 操作类型 枚举
* @throws FkptServiceException
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static void valid(Object t, OperateType optype) throws FkptServiceException {
try {
// 验证主的参数bean
validSingle(t, optype);
// 验证主bean中的list对象
Class cls = t.getClass();
for (Field field : cls.getDeclaredFields()) {
if (field.getType() == List.class) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cls);
Method getMethod = pd.getReadMethod();// 获得get方法
List<Object> list = (List<Object>) getMethod.invoke(t);
if (!CollectionUtils.isEmpty(list)) {
for (Object o : list) {
validSingle(o, optype);
}
}
}
}
} catch (Exception e) {
throw new FkptServiceException(e.getMessage(), e);
}
}
@SuppressWarnings("rawtypes")
public static void validSingle(Object t, OperateType optype) throws FkptServiceException {
StringBuilder err = new StringBuilder("");
// 获取注解信息
BeanVaild bv = null;
Class cls = t.getClass();
// 检测field是否存在
try {
Field[] fields = cls.getDeclaredFields();
for (Field f : fields) {
bv = f.getAnnotation(BeanVaild.class);
// 不为空执行校验
if (bv != null) {
// 新增验证
if (optype == OperateType.ADD && bv.addOp()) {
err.append(checkType(f, bv.type(), t));
} else if (optype == OperateType.UPDATE && bv.updateOp()) {
// 更新验证
err.append(checkType(f, bv.type(), t));
}
}
}
String errmsg = err.toString();
if (!StringUtil.isNullString(errmsg)) {
throw new FkptServiceException(errmsg);
}
} catch (Exception e) {
throw new FkptServiceException(ServiceExcuteMessage.FKPT_BEAN_VALID_001, e, t.getClass().getName(),
e.getMessage());
}
}
// 根据不同的验证类型执行验证操作
public static StringBuilder checkType(Field field, ValidTypes type, Object t) throws Exception {
StringBuilder err = new StringBuilder();
switch (type) {
case NOTNULL:
err.append(checkNotNull(field.getName(), type, t));
break;
default:
err.append("");
break;
}
return err;
}
// 非空验证
private static StringBuilder checkNotNull(String fieldName, ValidTypes type, Object t) throws Exception {
PropertyDescriptor pd = new PropertyDescriptor(fieldName, t.getClass());
Method getMethod = pd.getReadMethod();// 获得get方法
String fieldVal = (String) getMethod.invoke(t);
if (StringUtil.isNullString(fieldVal)) {
return new StringBuilder("获取【" + fieldName + "】失败,该字段不能为空\n");
} else {
return new StringBuilder("");
}
}
}
注解接口
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BeanVaild {
/**
* 验证类型
*/
ValidTypes type() default ValidTypes.NOTNULL;
/**
* 新增操作类型
*/
boolean addOp() default false;
/**
* 更新操作类型
*/
boolean updateOp() default false;
}
类型枚举
public enum OperateType {
/**
* 操作类型 新增
*/
ADD,
/**
* 操作类型 更新
*/
UPDATE
}
public enum ValidTypes {
/**
* 不能为空
*/
NOTNULL
}
javabean主类
public class Fxfa extends BaseBean {
private static final long serialVersionUID = 8239685879814948678L;
/**
* 方案编号
*/
@BeanVaild(type = ValidTypes.NOTNULL, updateOp = true)
private String fabh;
/**
* 方案名称
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String famc;
/**
* 方案类型
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true)
private String falx;
/**
* 优先级
*/
private String yxj;
/**
* 优先级名称
*/
private String yxjMc;
/**
* 版本
*/
private String bb;
/**
* 预警值测算方案
*/
private String yjzcsfa;
/**
* 预警值测算方案名称
*/
private String yjzcsfaMc;
/**
* 父方案代码,即上一版本方案代码
*/
private String fabhF;
/**
* 初方案代码,即初始版本方案代码
*/
private String fabhC;
/**
* 有效标志(Y、N)
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true)
private String yxbz;
/**
* 来源(1:系统自建;2:外部导入)
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true)
private String ly;
/**
* 制定人员代码
*/
private String zdRyDm;
/**
* 制定税务机关代码
*/
private String zdSwjgDm;
/**
* 制定时间
*/
private String zdsj;
/**
* 修改人员代码
*/
private String xgRyDm;
/**
* 修改税务机关代码
*/
private String xgSwjgDm;
/**
* 修改时间
*/
private String xgsj;
/**
* 关键词
*/
private String gjc;
/**
* 说明
*/
private String sm;
/**
* 纳税评估项目ID
*/
private String nspgxmid;
/**
* 纳税评估项目MC
*/
private String nspgxmmc;
/**
* 系统代码
*/
private String xtdm;
/**
* 分析模型列表
*/
private List<FxfaFxmxXx> lstFxmxXx = new ArrayList<FxfaFxmxXx>();
/**
* 指标元条件
*/
private List<FxfaZbyTj> lstZbyTj = new ArrayList<FxfaZbyTj>();
/**
* 维度条件列表
*/
private List<FxfaWdTj> lstWdTj = new ArrayList<FxfaWdTj>();
/**
* @return the lstZbyTj
*/
public List<FxfaZbyTj> getLstZbyTj() {
return lstZbyTj;
}
/**
* @param lstZbyTj
* the lstZbyTj to set
*/
public void setLstZbyTj(List<FxfaZbyTj> lstZbyTj) {
this.lstZbyTj = lstZbyTj;
}
/**
* @return the lstWdTj
*/
public List<FxfaWdTj> getLstWdTj() {
return lstWdTj;
}
/**
* @param lstWdTj
* the lstWdTj to set
*/
public void setLstWdTj(List<FxfaWdTj> lstWdTj) {
this.lstWdTj = lstWdTj;
}
/**
* @return the fabh
*/
public String getFabh() {
return fabh;
}
/**
* @param fabh
* the fabh to set
*/
public void setFabh(String fabh) {
this.fabh = fabh;
}
/**
* @return the famc
*/
public String getFamc() {
return famc;
}
/**
* @param famc
* the famc to set
*/
public void setFamc(String famc) {
this.famc = famc;
}
/**
* @return the falx
*/
public String getFalx() {
return falx;
}
/**
* @param falx
* the falx to set
*/
public void setFalx(String falx) {
this.falx = falx;
}
/**
* @return the yxj
*/
public String getYxj() {
return yxj;
}
/**
* @param yxj
* the yxj to set
*/
public void setYxj(String yxj) {
this.yxj = yxj;
}
/**
* @return the bb
*/
public String getBb() {
return bb;
}
/**
* @param bb
* the bb to set
*/
public void setBb(String bb) {
this.bb = bb;
}
/**
* @return the fabhF
*/
public String getFabhF() {
return fabhF;
}
/**
* @param fabhF
* the fabhF to set
*/
public void setFabhF(String fabhF) {
this.fabhF = fabhF;
}
/**
* @return the fabhC
*/
public String getFabhC() {
return fabhC;
}
/**
* @param fabhC
* the fabhC to set
*/
public void setFabhC(String fabhC) {
this.fabhC = fabhC;
}
/**
* @return the yxbz
*/
public String getYxbz() {
return yxbz;
}
/**
* @param yxbz
* the yxbz to set
*/
public void setYxbz(String yxbz) {
this.yxbz = yxbz;
}
/**
* @return the ly
*/
public String getLy() {
return ly;
}
/**
* @param ly
* the ly to set
*/
public void setLy(String ly) {
this.ly = ly;
}
/**
* @return the zdRyDm
*/
public String getZdRyDm() {
return zdRyDm;
}
/**
* @param zdRyDm
* the zdRyDm to set
*/
public void setZdRyDm(String zdRyDm) {
this.zdRyDm = zdRyDm;
}
/**
* @return the zdSwjgDm
*/
public String getZdSwjgDm() {
return zdSwjgDm;
}
/**
* @param zdSwjgDm
* the zdSwjgDm to set
*/
public void setZdSwjgDm(String zdSwjgDm) {
this.zdSwjgDm = zdSwjgDm;
}
/**
* @return the zdsj
*/
public String getZdsj() {
return zdsj;
}
/**
* @param zdsj
* the zdsj to set
*/
public void setZdsj(String zdsj) {
this.zdsj = zdsj;
}
/**
* @return the xgRyDm
*/
public String getXgRyDm() {
return xgRyDm;
}
/**
* @param xgRyDm
* the xgRyDm to set
*/
public void setXgRyDm(String xgRyDm) {
this.xgRyDm = xgRyDm;
}
/**
* @return the xgSwjgDm
*/
public String getXgSwjgDm() {
return xgSwjgDm;
}
/**
* @param xgSwjgDm
* the xgSwjgDm to set
*/
public void setXgSwjgDm(String xgSwjgDm) {
this.xgSwjgDm = xgSwjgDm;
}
/**
* @return the xgsj
*/
public String getXgsj() {
return xgsj;
}
/**
* @param xgsj
* the xgsj to set
*/
public void setXgsj(String xgsj) {
this.xgsj = xgsj;
}
/**
* @return the gjc
*/
public String getGjc() {
return gjc;
}
/**
* @param gjc
* the gjc to set
*/
public void setGjc(String gjc) {
this.gjc = gjc;
}
/**
* @return the yjzcsfa
*/
public String getYjzcsfa() {
return yjzcsfa;
}
/**
* @param yjzcsfa
* the yjzcsfa to set
*/
public void setYjzcsfa(String yjzcsfa) {
this.yjzcsfa = yjzcsfa;
}
/**
* @return the lstFxmxXx
*/
public List<FxfaFxmxXx> getLstFxmxXx() {
return lstFxmxXx;
}
/**
* @param lstFxmxXx
* the lstFxmxXx to set
*/
public void setLstFxmxXx(List<FxfaFxmxXx> lstFxmxXx) {
this.lstFxmxXx = lstFxmxXx;
}
/**
* @return the sm
*/
public String getSm() {
return sm;
}
/**
* @param sm
* the sm to set
*/
public void setSm(String sm) {
this.sm = sm;
}
/**
* @return the yjzcsfaMc
*/
public String getYjzcsfaMc() {
return yjzcsfaMc;
}
/**
* @param yjzcsfaMc
* the yjzcsfaMc to set
*/
public void setYjzcsfaMc(String yjzcsfaMc) {
this.yjzcsfaMc = yjzcsfaMc;
}
/**
* @return the yxjMc
*/
public String getYxjMc() {
return yxjMc;
}
/**
* @param yxjMc
* the yxjMc to set
*/
public void setYxjMc(String yxjMc) {
this.yxjMc = yxjMc;
}
/**
* @return 获取 nspgxmid
*/
public String getNspgxmid() {
return nspgxmid;
}
/**
* @param nspgxmid
* 设置 nspgxmid
*/
public void setNspgxmid(String nspgxmid) {
this.nspgxmid = nspgxmid;
}
/**
* @return 获取 nspgxmmc
*/
public String getNspgxmmc() {
return nspgxmmc;
}
/**
* @param nspgxmmc
* 设置 nspgxmmc
*/
public void setNspgxmmc(String nspgxmmc) {
this.nspgxmmc = nspgxmmc;
}
/**
* @return the xtdm
*/
public String getXtdm() {
return xtdm;
}
/**
* @param xtdm
* the xtdm to set
*/
public void setXtdm(String xtdm) {
this.xtdm = xtdm;
}
}
字bean
public class FxfaWdTj implements Serializable {
private static final long serialVersionUID = -7624527525952631847L;
/**
* 方案编号
*/
private String fabh;
/**
* 序号
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String xh;
/**
* 条件名称
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String tjmc;
/**
* 条件表
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String tjb;
/**
* 条件字段
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String tjzd;
/**
* 条件符
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String tjf;
/**
* 条件值
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String tjz;
/**
* 页面显示代码
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String xsdm;
/**
* 页面显示名称
*/
@BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true)
private String xsmc;
/**
* @return the fabh
*/
public String getFabh() {
return fabh;
}
/**
* @param fabh
* the fabh to set
*/
public void setFabh(String fabh) {
this.fabh = fabh;
}
/**
* @return the tjmc
*/
public String getTjmc() {
return tjmc;
}
/**
* @param tjmc
* the tjmc to set
*/
public void setTjmc(String tjmc) {
this.tjmc = tjmc;
}
/**
* @return the tjb
*/
public String getTjb() {
return tjb;
}
/**
* @param tjb
* the tjb to set
*/
public void setTjb(String tjb) {
this.tjb = tjb;
}
/**
* @return the tjzd
*/
public String getTjzd() {
return tjzd;
}
/**
* @param tjzd
* the tjzd to set
*/
public void setTjzd(String tjzd) {
this.tjzd = tjzd;
}
/**
* @return the tjf
*/
public String getTjf() {
return tjf;
}
/**
* @param tjf
* the tjf to set
*/
public void setTjf(String tjf) {
this.tjf = tjf;
}
/**
* @return the tjz
*/
public String getTjz() {
return tjz;
}
/**
* @param tjz
* the tjz to set
*/
public void setTjz(String tjz) {
this.tjz = tjz;
}
/**
* @return the xsdm
*/
public String getXsdm() {
return xsdm;
}
/**
* @param xsdm
* the xsdm to set
*/
public void setXsdm(String xsdm) {
this.xsdm = xsdm;
}
/**
* @return the xsmc
*/
public String getXsmc() {
return xsmc;
}
/**
* @param xsmc
* the xsmc to set
*/
public void setXsmc(String xsmc) {
this.xsmc = xsmc;
}
/**
* @return the xh
*/
public String getXh() {
return xh;
}
/**
* @param xh
* the xh to set
*/
public void setXh(String xh) {
this.xh = xh;
}
}
验证调用
// 新增
if (StringUtil.isNullString(fxfa.getFabh())) {
// 新增时验证参数
BeanValidUtil.valid(fxfa, OperateType.ADD);
fxfa.setFabh(UUIDUtil.nextVal());
ServiceExecuteUtil.executeDao("fxpt.core.fak.fxfa.dao.insertFxfa", new Object[] {fxfa, loginuser});
} else {
// 修改
// 修改时验证参数
BeanValidUtil.valid(fxfa, OperateType.UPDATE);
ServiceExecuteUtil.executeDao("fxpt.core.fak.fxfa.dao.updateFxfa", new Object[] {fxfa, loginuser});
}