BeanUtils工具类

  • 自定义数据类型使用BeanUtils工具时,本身必须具备getter和setter方法,因为BeanUtils工具本身也是一种内省的实现方法,所以也是借助于底层的getter和setter方法进行转换的。
API(org.springframework.beans.BeanUtils)用途
BeanUtils.copyProperties(bean1,bean2)将bean1赋值给2
API(org.apache.commons.beanutils.BeanUtils)用途
BeanUtils.copyProperties(newObject,oldObject)bean–>bean 实现对象的拷贝
BeanUtils.populate(bean,Map)map->bean 其中Map中的key必须与目标对象中的属性名相同,否则不能实现拷贝。
BeanUtils.describe(bean)bean–>map
BeanUtils.setProperty(bean, name, value)其中bean是指你将要设置的对象,name指的是将要设置的属性(写成”属性名”),value(从配置文件中读取到到的字符串值)
BeanUtils.copyProperty(bean, name, value)和上面的方法是完全一样的。使用哪个都可以
ConvertUtils.register(Converter converter , …)当需要将String数据转换成引用数据类型(自定义数据类型时),需要使用此方法实现转换。
void copyProperties(Object source, Object target)

可以对相同类型javaBean进行赋值,不同的也可以(这里规范检测不建议使用apache就用spring得了)

package com.wyy.controller;

import com.wyy.pojo.User;
import com.wyy.pojo.UserForm;
import com.wyy.pojo.UserVo;
import org.springframework.beans.BeanUtils;
//import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;

/**
 * @author yuanyuan.wang
 * @date 2019/12/18
 */
@Controller
public class test {

    @GetMapping("/test1")
    @ResponseBody
    public String test1() throws InvocationTargetException, IllegalAccessException {
        User user = new User();
        user.setLimitTimes( 11 );
        user.setStartTime( (long) 22 );
        user.setEndTime( (long) 33 );
        user.setId("66");
        user.setUsername("zj");
        user.setRole( "aaa" );
        user.setCreateTime( (long) 77 );
        user.setPassword("aaa");
        user.setNumber( 1 );
        user.setQuestions( "aaa" );
        user.setTestUrl( "url" );
        //以上为初始化

        //
        UserVo userVo2= new UserVo();
        BeanUtils.copyProperties(user,userVo2);
        return "0";
    }

    /**
     * 不同类不同对象之间的数据复制
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @GetMapping("/test2")
    @ResponseBody
    public String test2() throws InvocationTargetException, IllegalAccessException {
        /*userForm--->user
        UserForm userForm =  new UserForm();
        userForm.setUsername("zj");
        userForm.setPassword("aaa");
        //以上为初始化
        User user = new User();
        BeanUtils.copyProperties(userForm, user);*/

//        user--->userForm
        User user = new User();
        user.setLimitTimes( 11 );
        user.setStartTime( (long) 22 );
        user.setEndTime( (long) 33 );
        user.setId("66");
        user.setUsername("zj");
        user.setRole( "aaa" );
        user.setCreateTime( (long) 77 );
        user.setPassword("aaa");
        user.setNumber( 1 );
        user.setQuestions( "aaa" );
        user.setTestUrl( "url" );
        //以上为初始化
        
        UserForm userForm =  new UserForm();
        BeanUtils.copyProperties(user,userForm);
        return "2";
    }
}
public static Map<String,String> describe(Object bean)
public static void populate(Object bean, Map<String,? extends Object> properties)
package com.wyy.controller;

import com.wyy.pojo.User;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author yuanyuan.wang
 * @date 2019/12/18
 */
@Controller
public class testApache {

    /**
     * 对象数据和Map之间互相转化
     */
    @GetMapping("/test3")
    @ResponseBody
    public String test3() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        User user = new User();
        user.setLimitTimes( 11 );
        user.setStartTime( (long) 22 );
        user.setEndTime( (long) 33 );
        user.setId("66");
        user.setUsername("zj");
        user.setRole( "aaa" );
        user.setCreateTime( (long) 77 );
        user.setPassword("aaa");
        user.setNumber( 1 );
        user.setQuestions( "aaa" );
        user.setTestUrl( "url" );
        //以上为初始化

        //将对象转换为Map
        Map userMap = BeanUtils.describe(user);

        //将map转换为对象
        User user2 = new User();
        BeanUtils.populate(user2,userMap);
        return "3";
    }
}
  • 注:spring和apache的copyProperties参数顺序相反
  • 注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。
    apache官方API
    Spring官方API
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值