MCPcopy Create free account
hub / github.com/LisenCoding/cangku / CommonUtil

Class CommonUtil

boot/src/main/java/com/utils/CommonUtil.java:8–52  ·  view source on GitHub ↗

公共方法

Source from the content-addressed store, hash-verified

6 * 公共方法
7 */
8public class CommonUtil {
9 /**
10 * 获取随机字符串
11 *
12 * @param num
13 * @return
14 */
15 public static String getRandomString(Integer num) {
16 String base = "abcdefghijklmnopqrstuvwxyz0123456789";
17 Random random = new Random();
18 StringBuffer sb = new StringBuffer();
19 for (int i = 0; i < num; i++) {
20 int number = random.nextInt(base.length());
21 sb.append(base.charAt(number));
22 }
23 return sb.toString();
24 }
25
26
27 /**
28 * 检查map参数并添加缺失参数
29 * @param params
30 * @return
31 */
32 public static void checkMap(Map<String, Object> params) {
33 boolean page=true,limit=true,sort=true,order=true;
34 Iterator<Map.Entry<String, Object>> iter = params.entrySet().iterator();
35 while (iter.hasNext()) {
36 Map.Entry<String, Object> info = iter.next();
37 Object key = info.getKey();
38 Object value = info.getValue();
39 if(value==null || "".equals(value) || "null".equals(value)){
40 iter.remove();
41 }else if("page".equals(key)){page=false;
42 }else if("limit".equals(key)){limit=false;
43 }else if("sort".equals(key)){sort=false;
44 }else if("order".equals(key)){order=false;
45 }
46 }
47 if(page){params.put("page","1");}
48 if(limit){params.put("limit","10");}
49 if(sort){params.put("sort","id");}
50 if(order){params.put("order","desc");}
51 }
52}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected