效果图如下:
代码如下:层级分明 controller控制层 service serviceImpl业务层 dao持久层 xml
controller
@ApiOperation(value = "26个首字母排序") @RequestMapping(value = "alphabetical", method = RequestMethod.POST) public PmpResult alphabetical (){ //数据源查询数据库得出 List<CustomCabinets2> alphabeticalGather = crmCustomersService.alphabetical(); try { // 输出26个字母 TreeMap有序集合 Map<String, Object> map = new TreeMap<>(); for (int i = 1; i <= 26; i++) { //首字母 String word = String.valueOf((char)(96 + i)).toUpperCase(); // 循环找出 首字母一样的数据 List<Object> letter = new ArrayList<>(); //业务中查询出来的数据源 texts for (CustomCabinets2 customCabinets2 : alphabeticalGather) { //调用工具类方法获取汉字串拼音 String zm = AlphabeticalUtils.getFullSpell(customCabinets2.getCabinetsTwoType()).substring(0, 1); //将26个字母 和遍历出来接触的首字母比较相同添加到集合 if (word.equals(zm)) { letter.add(customCabinets2); } } map.put(word, letter); } return PmpResult.success(map); } catch (Exception e) { e.printStackTrace(); } return null; } 工具类AlphabeticalUtils : http://t.csdn.cn/pVm41 ServiceImpl @Override public List<CustomCabinets2> alphabetical() { List<CustomCabinets2> alphabetical = crmCustomersDao.alphabetical(); return alphabetical; } Service List<CustomCabinets2> alphabetical(); Dao List<CustomCabinets2> alphabetical(); XML <select id="alphabetical" resultType="com.lt.crm.entity.CustomCabinets2"> SELECT CABINETS_TWO_CODE "cabinetsTwoCode", CABINETS_TWO_TYPE "cabinetsTwoType", STATE "state" FROM CUSTOM_CABINETS2 </select>