1. 程式人生 > 實用技巧 >JAVA代理的使用

JAVA代理的使用

目的:根據配置資訊封裝mybatis的example。

1.要先進行欄位的格式封裝:首字母大寫的駝峰式命名

2.遍歷封裝後的欄位。使用代理獲取值還有獲取example封裝

    public MAcChannelSpotBoExample getExampleByVo(MAcChannelSpotVo vo) {
        /**
         *  獲取去重欄位,並封裝成list結構
         */
        MAcSpotRemDeplicateBo ruleBo =  mAcSpotRemDeplicateBoMapper.selectByPrimaryKey(vo.getRuleId());
        String[] removeDeplicates 
= ruleBo.getRemoveDeplicate().split(","); removeDeplicates = insert(removeDeplicates, "RULE_ID"); List<String> removeDeplicateLows = new ArrayList(); for(String s : removeDeplicates) { String ss = s.toLowerCase(); String [] str = ss.split("_"); StringBuffer newS
= new StringBuffer(); for(int i=0; i<str.length; i++) { newS.append(str[i].substring(0,1).toUpperCase()+str[i].substring(1)); } removeDeplicateLows.add(newS.toString()); } /** * 根據去重欄位+ruleId,動態封裝 example */ MAcChannelSpotBoExample example
= new MAcChannelSpotBoExample(); MAcChannelSpotBoExample.Criteria criteria = example.createCriteria(); if(StringUtils.isNotBlank(vo.getChannelSpotId())) { criteria.andChannelSpotIdNotEqualTo(vo.getChannelSpotId()); } Method[] m = criteria.getClass().getMethods(); Method[] m2 = vo.getClass().getMethods(); try { for (String str : removeDeplicateLows) { for (int i = 0; i < m2.length; i++) { if (("get" + str).toLowerCase().equals(m2[i].getName().toLowerCase())) { Object value2 = m2[i].invoke(vo); for(int j = 0; j < m.length; j++){ if(("and"+str + "EqualTo").toLowerCase().equals(m[j].getName().toLowerCase())){ m[j].invoke(criteria, value2); } } } } } } catch (Exception e) { LOG.error(e); } return example; }