关于枚举映射的记录贴

创建枚举

package org.jeecg.modules.coretool.enumerate;


/**
 * 功能对应的 service 类 ,
 * 通过反射查询表数据
 */

public enum FuncTable {

    Demo("demo","spss_demo","org.jeecg.modules.spss.entity.SpssDemo","org.jeecg.modules.spss.service.ISpssDemoService"),
    
    private final String funcCode;
    private final String tableName;
    private final String entityName;
    private final String serviceName;


    FuncTable(String funcCode,String tableName,String entityName,String serviceName){

        this.funcCode = funcCode;
        this.tableName = tableName;
        this.entityName = entityName;
        this.serviceName = serviceName;
    }

    public static String getTableNameByCode(String funcCode){

        for (FuncTable funcTable : FuncTable.values()){
            if (funcTable.funcCode.equals(funcCode)){
                return funcTable.tableName;
            }

        }
        return "";
    }

    public static String getServiceNameByCode(String funcCode){
        for (FuncTable funcTable : FuncTable.values()){
            if (funcTable.funcCode.equals(funcCode)){
                return funcTable.serviceName;
            }
        }
        return "";
    }
    public static String getEntityNameByCode(String funcCode){
        for (FuncTable funcTable : FuncTable.values()){
            if (funcTable.funcCode.equals(funcCode)){
                return funcTable.entityName;
            }
        }
        return "";
    }

    public String getFuncCode() {
        return funcCode;
    }

    public String getServiceName() {
        return serviceName;
    }

}

通过funcCode调用

try {
//通过反射获取service 类 ,entity 类
serviceClz = Class.forName(FuncTable.getServiceNameByCode(funcCode));
entityClz = Class.forName(FuncTable.getEntityNameByCode(funcCode));

        Object o = entityClz.newInstance();
        ReflectHelper reflectHelper = new ReflectHelper(o);
        reflectHelper.setAll(jsonObject.getInnerMap());

        //在json中删除已经设置的属性,剩下的都是自定义的属性
        Field[] fields = entityClz.getDeclaredFields();
        for (Field f:fields) {
            jsonObject.remove(f.getName());
        }

        //设置自定义字段保存
        reflectHelper.setMethodValue("extra",jsonObject.toJSONString());
        //2  判断是否有流程,启动流程
        if ("1".equals(funcConf.getEnableProc())){
            try{
                Integer procInstId = wfInstService.startProcessInstanceByID(funcConf.getId(),params);
                //业务表回写流程实例id
                reflectHelper.setMethodValue("wfInstId",procInstId);
            }catch (Exception e){
                e.printStackTrace();
                return e.getMessage();
            }

        }
        //调用保存方法
        Method saveMethod = serviceClz.getMethod("saveOrUpdate", Object.class);
        Object serviceObject= SpringContextUtils.getBean(serviceClz);
        saveMethod.invoke(serviceObject,o);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return funcCode+"未找到对应配置的service";
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (InstantiationException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        return e.getMessage();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值