1128XF

实现局部更新表中的字段

  在项目开发过程中,发现在更新用户信息时,PC端更新操作与App端更新操作流程不一致,导致原来对用户信息表中的字段全部更新的接口不适合App端的调用需求,由此衍生出,如何实现对表中局部字段的更新,比如,只更新用户性别及只更新用户头像,而又或者对用户信息的字段的全部更新操作。在这里我想到了,通过对Bean中的每个属性添加对应的注解,注解的value即为表中的字段Code,再通过反射拼接出最终所需要的SQL。

 

 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnoField {
    String value();
    String dataType() default "String";//"DATE" 代表日期类型
    boolean allowNull() default false;
}

 

 

 

/**
     * 通行证密码(只有本地用户有)
     */
    @AnnoField("USER_PSW")
    private String userpsw;
    /**
     * 通行证状态 1:正常  0:停用
     */
    private String status;
    /**
     * 用户名称(昵称)
     */
    @AnnoField("NICK_NAME")
    private String nickName;
    /**
     * 手机号码
     */
    @AnnoField("USER_TEL")
    private String userTel;
/**
     * 更新数据库表数据
     * @param object 目标表映射的实体类对象
     * @param gid 更新目标表中的主键Gid
     * @param clazz 目标表映射的实体类字节码对象
     * @param paramsList 
     * @param sql 
     * @param conditionField  where条件字段
     * xufan
     */
    public static void updateTableData(Object object, String gid,Class clazz,List<Object> paramsList,StringBuffer sql,String conditionField) {

        Field[] fields = ReflectUtils.getAllFields(clazz);

        for (Field field : fields) {
            String name = field.getName();
            AnnoField annotation = field.getAnnotation(AnnoField.class);
            if (annotation != null) {
                String value = annotation.value();
                String dataType = annotation.dataType();
                boolean allowNull = annotation.allowNull();
                try {
                    Object propertyValue = ReflectUtils.getPropertyValue(object, name);
                    if("DATE".equals(dataType)){
                        Date date = new Date();
                        if(propertyValue!=null){
                            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            date = df.parse(propertyValue.toString());
                        }
                            propertyValue = date;
                    }
                    if (propertyValue != null) {
                        sql.append("T." + value + "=?,");
                            paramsList.add(propertyValue);
                    }else{
                        if(allowNull){
                            sql.append("T." + value + "=null,");
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(" WHERE T."+(StringUtils.isNotNull(conditionField)?conditionField:"GID")+"= ?");
        paramsList.add(gid);
        
    }

 

 

/**
     * 更新用户基本信息
     * 
     * @param updateUserInfo
     * @return xufan
     */
    @Override
    public boolean updatePassportInfo(PassportInfoBO passportInfoBO) {
        
        String passportGid = passportInfoBO.getGid();
        if (StringUtils.isNull(passportGid)){
            return false;
        }
        
        StringBuffer sql = new StringBuffer();
        List<Object> paramsList = new ArrayList<Object>();
        sql.append("UPDATE PASSPORT_INFO T SET ");
        ReflectUtils.updateTableData(passportInfoBO,passportGid,PassportInfoBO.class,paramsList,sql,null);
        if (paramsList.size() > 1) {
            return jdbcQuery.update(BaseConfig.DB_MAIN, sql.toString(), paramsList.toArray());
        }
        return true;
    }

 

posted on 2017-03-18 19:31  1128XF  阅读(252)  评论(0编辑  收藏  举报

导航