首页 > 代码库 > 几个常用的公共类

几个常用的公共类

返回结果的类ResultMessage

  

技术分享
 1 package com.puyun;
 2 
 3 import org.springframework.util.StringUtils;
 4 
 5 public class ResultMessage {
 6     public static final String SUCCESS_CODE = "200";
 7     public static final String ERROR_CODE = "500";
 8 
 9     private String code;
10     private String error;
11     private Object result;
12 
13     public ResultMessage() {
14 
15     }
16 
17     public ResultMessage(String code, String error, Object result) {
18         this.code = code;
19         this.error = error;
20         this.result = result;
21     }
22 
23 
24     /**
25      * 返回成功的结果信息
26      *
27      * @param msg
28      * @param result
29      * @return
30      */
31     public static ResultMessage success(String msg, Object result) {
32         if(StringUtils.isEmpty(msg)){
33             msg="操作成功";
34         }
35         return new ResultMessage(SUCCESS_CODE, msg, result);
36     }
37 
38     /**
39      * 返回错误的结果信息
40      *
41      * @param msg
42      * @param result
43      * @return
44      */
45     public static ResultMessage error(String msg, Object result) {
46         if(StringUtils.isEmpty(msg)){
47             msg="操作失败";
48         }
49         return new ResultMessage(ERROR_CODE, msg, result);
50     }
51 
52     public String getCode() {
53         return code;
54     }
55 
56     public void setCode(String code) {
57         this.code = code;
58     }
59 
60     public String getError() {
61         return error;
62     }
63 
64     public void setError(String error) {
65         this.error = error;
66     }
67 
68     public Object getResult() {
69         return result;
70     }
71 
72     public void setResult(Object result) {
73         this.result = result;
74     }
75 }
View Code

随机数的类

技术分享
 1 package com.puyun.common.util;
 2 
 3 import java.text.DateFormat;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Calendar;
 6 import java.util.Date;
 7 import java.util.Random;
 8 import java.util.UUID;
 9 
10 /**
11  * Created by huangjianbo on 2017/6/6 0006.
12  */
13 public class RandomUtils {
14     private static char[] numbersAndLetters = ("0123456789").toCharArray();
15     /**
16      * 不重复的订单号
17      * @return
18      */
19     public static String getOrderIdByUUId() {
20         DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
21         int first = new Random(10).nextInt(8) + 1;
22         int hashCodeV = UUID.randomUUID().toString().hashCode();
23         if (hashCodeV < 0) {//有可能是负数
24             hashCodeV = -hashCodeV;
25         }
26         // 0 代表前面补充0
27         // 4 代表长度为4
28         // d 代表参数为正数型
29         String result = dateFormat.format(new Date()) + first + String.format("%015d", hashCodeV);
30         return result;
31     }
32 
33 
34     /**
35      * 生成 字母+数组的随机数
36      *
37      * @param length
38      *
39      * @return
40      */
41     public static String randomStringValue(int length) {
42         Random randGen = new Random();
43         if (length < 1) {
44             return null;
45         }
46         char[] randBuffer = new char[length];
47         for (int i = 0; i < randBuffer.length; i++) {
48             randBuffer[i] = numbersAndLetters[randGen.nextInt(10)];
49         }
50         return new String(randBuffer);
51     }
52     /**
53      * 获取指定时间的那天 23:59:59.999 的时间
54      *
55      * @param date
56      * @return
57      */
58     public static Date dayEnd(final Date date) {
59         Calendar c = Calendar.getInstance();
60         c.setTime(date);
61         c.set(Calendar.HOUR_OF_DAY, 23);
62         c.set(Calendar.MINUTE, 59);
63         c.set(Calendar.SECOND, 59);
64         c.set(Calendar.MILLISECOND, 999);
65         return c.getTime();
66     }
67 }
View Code

 BaseService接口

技术分享
 1 package com.puyun.common.service;
 2 
 3 
 4 import java.util.List;
 5 
 6 public interface BaseService<T> {
 7     /**
 8      * &#x65b0;&#x589e;
 9      *
10      * @param po
11      * @return
12      */
13     public T insert(T po);
14 
15     /**
16      * 查询单一数据
17      *
18      * @param id
19      * @return
20      */
21     public T get(int id);
22 
23     /**
24      * 查询所有数据
25      *
26      * @return
27      */
28     public List<T> queryAll();
29 
30     /**
31      * 删除单一数据
32      *
33      * @param id
34      */
35     public void delete(int id);
36 
37     /**
38      * 更新对象
39      *
40      * @param po
41      */
42     public void update(T po);
43 
44 }
View Code

 

几个常用的公共类