Blame view

src/main/java/com/huaheng/common/utils/DataUtils.java 8.26 KB
tangying authored
1
2
package com.huaheng.common.utils;
mahuandong authored
3
import java.io.UnsupportedEncodingException;
tangying authored
4
5
6
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
mahuandong authored
7
import java.net.URLEncoder;
tangying authored
8
9
10
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
mahuandong authored
11
import java.util.Map;
tangying authored
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

public class DataUtils {

    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /***
     *  转化为Example方法名
     * @param filedName  字段名
     * @param methodSuffix  方法后缀
     * @return
     */
    public static String ConvertMethodName(String filedName, String methodSuffix) {
        String returnValue = null;
        if (filedName.length() > 0)
        {
            char[] nameChar = filedName.toCharArray();
            if (nameChar[0] >= 'a' && nameChar[0] <= 'z') {
                nameChar[0] = (char) (nameChar[0] - 32);
            }
            returnValue = "and" +   String.valueOf(nameChar) + methodSuffix;
        }
        return returnValue;
    }

    /**
     *  将对象转为字符串
     * @param object
     * @return
     */
    public static String getString(Object object)
    {
xqs authored
43
44
45
46
47
        if (object == null) {
            return null;
        } else {
            return object.toString();
        }
tangying authored
48
49
50
51
52
53
54
55
56
    }

    /**
     *  将对象转为Integer
     * @param object
     * @return
     */
    public static Integer getInteger(Object object)
    {
xqs authored
57
58
59
60
61
        if (object == null) {
            return null;
        } else {
            return Integer.valueOf(object.toString());
        }
tangying authored
62
63
64
65
66
67
68
69
70
    }

    /**
     *  将对象转为BigDecimal
     * @param object
     * @return
     */
    public static BigDecimal getBigDecimal(Object object)
    {
xqs authored
71
72
73
74
75
        if (object == null) {
            return null;
        } else {
            return new BigDecimal(object.toString());
        }
tangying authored
76
77
78
79
80
81
82
83
84
    }

    /**
     *  将对象转为Double
     * @param object
     * @return
     */
    public static Double getDouble(Object object)
    {
xqs authored
85
86
87
88
89
        if (object == null) {
            return null;
        } else {
            return new Double(object.toString());
        }
tangying authored
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    }

    /**
     *  long转为Integer
     * @param object
     * @return
     */
    public static Integer getInteger(long object)
    {
        return Integer.valueOf(String.valueOf(object));
    }

    /**
     *  将对象转为Date
     * @param object
     * @return
     */
    public static Date getDateTime(Object object)  throws ParseException
    {
        if (object == null)
        {
            return null;
        }
        else
        {
            return format.parse(object.toString());
        }
    }

    /**
     * 通过一个实体类,给另一个实体类赋值
     * @param source  源实体类
     * @param target  目标实体类
     * @param ignoreProperties 忽略字段(多个字段用逗号隔开)
     * @throws Exception
     */
    public static void CopyDataByNotNull (Object source, Object target, String ignoreProperties) throws Exception {
        Field[] sourceFields  =  source.getClass().getDeclaredFields();
        Field[] targetFields  =  target.getClass().getDeclaredFields();
        Field.setAccessible(targetFields, true);

        for (int i = 0; i < sourceFields.length; i++)   {
            String sourceName = sourceFields[i].getName();
            Object sourceValue = sourceFields[i].get(source);
            if (sourceValue == null || ignoreProperties.indexOf(sourceName) > -1) {
                continue;
            }
            for (Field targetField : targetFields) {
                if (targetField.getName().equals(sourceName)) {
                    targetField.set(target, sourceValue);
                    break;
                }
            }
        }
    }
mahuandong authored
146
147
148
149
150
151
    /**
     * 格式化map
     * @param map
     * @return
     */
    public static String sendGetFormat(Map<String, Object> map){
tangying authored
152
mahuandong authored
153
154
155
156
157
158
        StringBuilder reslut = new StringBuilder();
        for (Map.Entry<String, Object> a : map.entrySet()) {
            reslut.append(a.getKey()).append("=").append(EncodingUtil.encodeURIComponent(String.valueOf(a.getValue()))+"&");
        }
        return String.valueOf(reslut);
    }
tangying authored
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

//    /**
//     *    根据传过来实体类,自动拼接查询Condition,如果是字符类型就用LIKE查询,如果是日期类型就用>=或是<=,其他的用=
//     * @param entityClass
//     * @return
//     * @throws Exception
//     */
//    public static <T> Condition  createCondition (T entityClass) throws Exception {
//        Condition condition = new Condition(entityClass.getClass());
//        Field[] fields   =   entityClass.getClass().getDeclaredFields();
//        Field.setAccessible(fields, true);
//        for (int i = 0; i < fields.length; i++)   {
//                String filedName = fields[i].getName();
//                Object filedValue = fields[i].get(entityClass);
//                if (filedValue != null && filedValue.toString().length() > 0) {
//                        if (filedValue.getClass() == String.class) {
//                                condition.and().andLike(filedName, "%" + filedValue.toString() + "%");
//                        } else if(filedValue.getClass() == Date.class){
//                                if (filedName.startsWith("begin") || filedName.startsWith("create")) {
//                                    condition.and().andGreaterThanOrEqualTo(filedName, filedValue);
//                                }else if (filedName.startsWith("end")|| filedName.startsWith("update")){
//                                    condition.and().andLessThanOrEqualTo(filedName, filedValue);
//                                }
//                        } else {
//                            condition.and().andEqualTo(filedName, filedValue);
//                        }
//                }
//        }
//        return  condition;
//    }

//    /**
//     * 根据传过来实体类,自动拼接查询Example,如果是字符类型就用LIKE查询,如果是日期类型就用>=或是<=,其他的用=
//     * @param entity  实体
//     * @param example  需要生成的Example
//     * @param <T>  实体类型
//     * @param <V>  Example类型
//     * @throws Exception
//     */
//    public static <T, V> void createCriteria (T entity, V example) throws Exception {
//        Field[] entityFields   =   entity.getClass().getDeclaredFields();
//        Field.setAccessible(entityFields, true);
//        Object criteria = example.getClass().getDeclaredMethod("createCriteria").invoke(example);
//        Method[] exampleMethod = criteria.getClass().getDeclaredMethods();
//        for (int i = 0; i < entityFields.length; i++)   {
//            String filedName = entityFields[i].getName();
//            Object filedValue = entityFields[i].get(entity);
//            if (filedName.equals("deleted"))      filedValue = false;
//            if (filedName.equals("warehouseCode"))      filedValue = ShiroUtils.getWarehouseCode();
//            if (filedValue != null && filedValue.toString().length() > 0) {
//                String methodName = null;
//                if (filedValue.getClass() == Integer.class || filedValue.getClass() == Boolean.class || filedName.equals("warehouseCode"))
//                {
//                    methodName = ConvertMethodName(filedName, "EqualTo");
//                }
//                else if (filedValue.getClass() == String.class)
//                {
//                    methodName = ConvertMethodName(filedName, "Like");
//                    filedValue = filedValue.toString() + "%";
//                }
//                else if(filedValue.getClass() == Date.class)
//                {
//                    if (filedName.startsWith("begin") || filedName.startsWith("create")) {
//                        methodName = ConvertMethodName(filedName, "GreaterThanOrEqualTo");
//                    }else if (filedName.startsWith("end")|| filedName.startsWith("update")){
//                        methodName = ConvertMethodName(filedName, "andAddress1LessThanOrEqualTo");
//                    }
//                }
//                for (Method itemMethod : exampleMethod)
//                {
//                    if (itemMethod.getName().equals(methodName)) {
//                        itemMethod.invoke(criteria, filedValue);
//                    }
//                }
//            }
//        }
//    }


}