2012-10-18 79 views
0

我有一些值從數據庫返回的結果集類似下面無法組基於某些條件的一些值

**resource_name   menu_name  menu_group_name** 
DepartmentAction  Department  Admin Operation 
PositionAction   Position  Admin Operation 
FoodHabitAction   FoodHabits  Admin Operation 
ReligiousAction   Religious  Admin Operation 
NationalitiesAction  Nationlities Admin Operation 

我想組RESOURCE_NAME和MENU_NAME基於menu_group_name像下面

一些事情

如果menu_group_name與所有相應的 resource_name和menu_mane相對於menu_group_name相同。

這是我試過的方法,

public Map<String,List> getMenuForLoggedinRole(int roleid){ 
     Map<String,List> menuListMap = new LinkedHashMap<String,List>(); 
     List<MenuListViewModel> menuNamesList = new ArrayList<MenuListViewModel>(); 
     MenuListViewModel menuViewModel; 
     Connection connection = getConnection(); 
     PreparedStatement ps = null; 
     ResultSet rs = null; 
     if (connection != null) { 
      try { 
       ps = connection.prepareStatement(" select ar.resource_name,ar.menu_name,mg.menu_group_name " 
         + " from m_application_resources as ar," 
         + " m_menu_groups as mg,m_access_matrix as amatrix " 
         + " where ar.resourceid = amatrix.resourceid and amatrix.roleid=?"); 
       ps.setInt(1, roleid); 
       rs = ps.executeQuery(); 
       if(rs.next()) { 
        String menu_group_name = rs.getString("menu_group_name"); 
        String resource_name = rs.getString("resource_name"); 
        String menu_name = rs.getString("menu_name"); 
        if(menuListMap.containsKey(menu_group_name)){ 
         menuNamesList =(List) menuListMap.get(menu_group_name); 
         menuViewModel = new MenuListViewModel(); 
         menuViewModel.setResource_name(resource_name); 
         menuViewModel.setMenu_name(menu_name); 
         menuNamesList.add(menuViewModel); 
         menuListMap.put(menu_group_name, menuNamesList); 
        }else{ 
         menuViewModel = new MenuListViewModel(); 
         menuViewModel.setResource_name(resource_name); 
         menuViewModel.setMenu_name(menu_name); 
         menuNamesList.add(menuViewModel); 
         menuListMap.put(menu_group_name, menuNamesList); 
        } 
       } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      }finally { 
       try { 
        closeConnection(connection, rs, ps); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
        //use logger here 
       } 
      } 
     } 

     printMap(menuListMap); 
     return menuListMap; 
    } 

但它不給我disired輸出。

我的視圖模型:

public class MenuListViewModel { 

    private String resource_name = ""; 
    private String menu_name = ""; 

    public String getResource_name() { 
     return resource_name; 
    } 

    public void setResource_name(String resource_name) { 
     this.resource_name = resource_name; 
    } 

    public String getMenu_name() { 
     return menu_name; 
    } 

    public void setMenu_name(String menu_name) { 
     this.menu_name = menu_name; 
    } 

} 

public static void printMap(Map mp) { 
     List menuList = new ArrayList(); 
     MenuListViewModel model; 
     Iterator it = mp.entrySet().iterator(); 
     while (it.hasNext()) { 
      Map.Entry pairs = (Map.Entry)it.next(); 
      menuList =(List) pairs.getValue(); 
      for(int i=0;i<=menuList.size();i++){ 
       model =(MenuListViewModel) menuList.get(i); 
       System.out.println("Resource_name : "+model.getResource_name()); 
       System.out.println("Menu_name : "+model.getMenu_name()); 
      } 
      System.out.println(pairs.getKey() + " = " + pairs.getValue()); 
      it.remove(); // avoids a ConcurrentModificationException 
     } 
} 

請幫助。

回答

1

如果鍵不包含在映射中,那麼你實例化新的List。否則,如果存在,它將使用先前的列表對象。

    if(menuListMap.containsKey(menu_group_name)){ 
         menuNamesList =(List) menuListMap.get(menu_group_name); 
         menuViewModel = new MenuListViewModel(); 
         menuViewModel.setResource_name(resource_name); 
         menuViewModel.setMenu_name(menu_name); 
         menuNamesList.add(menuViewModel); 
         menuListMap.put(menu_group_name, menuNamesList); 
        }else{ 
         menuNamesList = new ArrayList<MenuListViewModel>(); 
         menuViewModel = new MenuListViewModel(); 
         menuViewModel.setResource_name(resource_name); 
         menuViewModel.setMenu_name(menu_name); 
         menuNamesList.add(menuViewModel); 
         menuListMap.put(menu_group_name, menuNamesList); 
        } 
+1

Thanks @Quoi,它現在正在工作,我在我的代碼中犯了錯誤。而不是迭代從結果集迭代我給出的If條件的值。 –

+0

這種方法省略了第一個值並給出了剩餘的值。如何避免這種情況? –

+0

它不應該。你調試過代碼嗎? –