代码段记录

// 通过属性获取传入对象的指定属性的值
public String getValueByPropName(Student student, String propName) {
    String value = null;
    try {
        // 通过属性获取对象的属性
        Field field = student.getClass().getDeclaredField(propName);
        // 对象的属性的访问权限设置为可访问
        field.setAccessible(true);
                    // 获取属性的对应的值
        value = field.get(student).toString();
    } catch (Exception e) {
        return null;
    }
     
    return value;
}

 

// 上一个的升级版,批量传来字段返回一个map 
// 通过属性获取传入对象的指定属性的值
    public static HashMap<String,Object> getValueByPropName(Student student, ArrayList<String> propName) {
        HashMap<String, Object> map = new HashMap<>();
        String value = null;
        try {
            for (int i = 0; i < propName.size(); i++) {
                // 通过属性获取对象的属性
                Field field = student.getClass().getDeclaredField(propName.get(i));
                // 对象的属性的访问权限设置为可访问
                field.setAccessible(true);
                // 获取属性的对应的值
                value = field.get(student).toString();
                String clazz = field.get(student).getClass().toString();

                if("class java.lang.String".equals(clazz)){
                    map.put(propName.get(i),value);
                }
                if("class java.lang.Integer".equals(clazz)){
                    map.put(propName.get(i),new Integer(value));
                }
                if(clazz.equals("class java.lang.Double")){
                    map.put(propName.get(i),new Double(value));
                }
                if(clazz.equals("class java.util.Date")){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    map.put(propName.get(i),sdf.parse(value));
                }
            }
        } catch (Exception e) {
            return null;
        }
        return map;
    }

 

<!-- 批量更新状态 -->
    <update id="updateBatch"> 
        update schedule_job set status = #{status} where job_id in 
        <foreach item="jobId" collection="list"  open="(" separator="," close=")">
            #{jobId}
        </foreach>
    </update>

 

// [{"name":"张三1"},{"name":"张三2"}] 
//  要将Json类型还原成对象,常用new TypeReference<过去的类型>
    List<Person> getlistPerson = mapper1.readValue(jsonPerson,new TypeReference<List<Person>>() {});

 

posted @ 2019-08-28 09:06  胡成长  阅读(168)  评论(0)    收藏  举报