反射映射实体类进行装配

反射映射实体类进行装配

package com.erju.springbootStudy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import lombok.Data;

@SpringBootTest
@SuppressWarnings("all")
class SpringbootStudyApplicationTests {

	@Data
    static class UserInfo {
        // @TableId(value = "id", type = IdType.AUTO)
        private Integer id;
        // @ApiModelProperty(value = "用户id")
        private String userId;
        // @ApiModelProperty(value = "用户昵称")
        private String userName;
        // @ApiModelProperty(value = "正式使用的图片,数组格式")
        private String img;
        // @ApiModelProperty(value = "1-男 2-女")
        private Integer gender;
        // @ApiModelProperty(value = "生日")
        private LocalDate birthday;
        // @ApiModelProperty(value = "身高-cm")
        private Integer height;
        // @ApiModelProperty(value = "家乡,如:浙江省-杭州市-西湖区")
        private String hometown;

    }

    /**
     * 组装参数,返回
     *
     * @throws NoSuchFieldException
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static UserInfo buildUserInfo(Map userInfoMap) throws Exception{
        UserInfo userInfo = new UserInfo();
        /** 请补充代码,将userInfoMap传入的参数,转化为userInfo */
        /**
         * 注意:此处传入的参数可能为任意的其中一个,非固定为birthday哦,原则上前端每次只会传入一个用户信息参数,但若传入多个,后端也只会保存其中一个参数
         */
        //获取实体类
        Class uInfo = userInfo.getClass();
        //获取map中的一个数据
        Set keySet = userInfoMap.keySet();
        Iterator iterator = keySet.iterator();
        String next = (String) iterator.next();
        //使用反射将数据注入倒实体类
        //将方法名首字母大写
        String first = next.substring(0,1).toUpperCase();
        //对传入数据的类型进行处理
        Class paramType = Class.forName(uInfo.getDeclaredField(String.valueOf(next)).getType().getName());
        //使用反射对传入的数据进行封装注入
        Method method = uInfo.getMethod("set"+first+next.substring(1),paramType);
        String name = paramType.getName();
        if(("java.lang.Integer").equals(name) ){
            method.invoke(userInfo,Integer.parseInt((String) userInfoMap.get(next)));
        }
        if(("java.time.LocalDate").equals(name)){
            method.invoke(userInfo,LocalDate.parse((String) userInfoMap.get(next)));
        }else
        {
            method.invoke(userInfo,userInfoMap.get(next));
        }
        //返回
        return userInfo;
    }

    @Test
    void testLogin() throws Exception {
    	UserInfo userInfo = new UserInfo();
        Map userInfoMap = new HashMap();
        userInfoMap.put("birthday", "1999-01-01");
        // userInfoMap.put("userName","用户名XXX");
        System.out.println(buildUserInfo(userInfoMap));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值