beanUtils操作总结

2024-08-20 17:32
文章标签 beanutils 总结 操作

本文主要是介绍beanUtils操作总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//使用beanUtils操作bean的属性(第三方)
public class BeanUtilsDemo {

 @Test
 public void test1() throws IllegalAccessException, InvocationTargetException
 { 
  Person1 p1=new Person1();
  
  //BeanUtils只能操作 public 修饰的class
  BeanUtils.setProperty(p1, "name", "p1name");
  System.out.println(p1.getName());
 }
 @Test
 public void test2() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
 { 
  String name="aaaa";
  String password="123";
  String age="34";
  String birthday="1990-11-09";
  
  Person1 p1=new Person1();
  
  //仅支持8种基本数据类型转换!!
  BeanUtils.setProperty(p1, "name", name);
  BeanUtils.setProperty(p1, "password", password);
  BeanUtils.setProperty(p1, "age", age);
  
  System.out.println(p1.getName());
  System.out.println(p1.getPassword());
  System.out.println(p1.getAge());
  
  //老张代码    birthday 为复合属性,支持属性的级联操作!! BeanUtils还可以操作Map;
  //BeanUtils.setProperty(p1, "birthday.time", birthday);
  //System.out.println(BeanUtils.getProperty(p1, "birthday"));
  
  
  //为了让日期附到bean的birthday属性上, 我们给beanUtils注册一个日期转换器
  //ConvertUtils.register(converter, clazz);
  ConvertUtils.register(new Converter(){  //匿名内部类
   @Override
   public Object convert(Class type, Object value) {
    
    //使用数据时,先检查后使用!!
    if(value==null)
     return null;
    if(!(value instanceof String))
    {
     throw new ConversionException("只支持String类型转换");
    }
    
    String str=(String) value;
    if(str.trim().equals(""))
     return null;
    
    //SimpleDateFormat 使得可以选择任何用户定义的日期-时间格式的模式
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    try {
     //只能是抓取异常,不能抛出比父类更多的异常;
     return sdf.parse(str);
    } catch (ParseException e) {
     // TODO Auto-generated catch block
     
     //若出现异常,封装数据抛给jvm控制台!!异常连不能掉;
     throw new RuntimeException(e);
    }
   }
  },Date.class);
  
  //使用自定义date 的转换器!!!
  BeanUtils.setProperty(p1, "birthday", birthday);
//  Date date=new Date();
//  System.out.println(date.toLocaleString());
  Date date=p1.getBirthday();  //英文时间格式
  String d=date.toLocaleString();//将转换中文格式!!
  System.out.println("英文格式:"+date+"中文格式:"+d);
 }
 @Test
 public void test3() throws IllegalAccessException, InvocationTargetException
 { 
  String name="aaaa";
  String password="123";
  String age="34";
  String birthday="1990-12-30";
  
  Person1 p1=new Person1();
  
  //仅支持8种基本数据类型转换!!
  BeanUtils.setProperty(p1, "name", name);
  BeanUtils.setProperty(p1, "password", password);
  BeanUtils.setProperty(p1, "age", age);
  
  //beanUtils自身 实现的转换器!! 当时空字符串时会抛异常!!
  //ConvertUtils.register(new DateLocaleConverter(), Date.class);
  
  System.out.println(p1.getName());
  System.out.println(p1.getPassword());
  System.out.println(p1.getAge());
  
  BeanUtils.setProperty(p1, "birthday", birthday);
//  Date date=new Date();
//  System.out.println(date.toLocaleString());
  Date date=p1.getBirthday();  //英文时间格式
  String d=date.toLocaleString();//将转换中文格式!!
  System.out.println("英文格式:"+date+"中文格式:"+d);
 }
 
 @Test
 public void test4() throws IllegalAccessException, InvocationTargetException
 { 
  //客户端以map形式提交数据
  Map map=new HashMap();
  map.put("name", "aaaa");
  map.put("password", "123");
  map.put("age", "23");
  map.put("birthday", "1990-12-30");
  
  ConvertUtils.register(new DateLocaleConverter(), Date.class);
  
  Person1 p1=new Person1();
  //用map集合填充bean的属性!!
  BeanUtils.populate(p1, map);
  
  System.out.println(p1.getName());
  System.out.println(p1.getPassword());
  System.out.println(p1.getAge());
  System.out.println(p1.getBirthday());
 }
 
}

这篇关于beanUtils操作总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090714

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

二分最大匹配总结

HDU 2444  黑白染色 ,二分图判定 const int maxn = 208 ;vector<int> g[maxn] ;int n ;bool vis[maxn] ;int match[maxn] ;;int color[maxn] ;int setcolor(int u , int c){color[u] = c ;for(vector<int>::iter

整数Hash散列总结

方法:    step1  :线性探测  step2 散列   当 h(k)位置已经存储有元素的时候,依次探查(h(k)+i) mod S, i=1,2,3…,直到找到空的存储单元为止。其中,S为 数组长度。 HDU 1496   a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 。 x在 [-100,100] 解的个数  const int MaxN = 3000

状态dp总结

zoj 3631  N 个数中选若干数和(只能选一次)<=M 的最大值 const int Max_N = 38 ;int a[1<<16] , b[1<<16] , x[Max_N] , e[Max_N] ;void GetNum(int g[] , int n , int s[] , int &m){ int i , j , t ;m = 0 ;for(i = 0 ;

go基础知识归纳总结

无缓冲的 channel 和有缓冲的 channel 的区别? 在 Go 语言中,channel 是用来在 goroutines 之间传递数据的主要机制。它们有两种类型:无缓冲的 channel 和有缓冲的 channel。 无缓冲的 channel 行为:无缓冲的 channel 是一种同步的通信方式,发送和接收必须同时发生。如果一个 goroutine 试图通过无缓冲 channel

9.8javaweb项目总结

1.主界面用户信息显示 登录成功后,将用户信息存储在记录在 localStorage中,然后进入界面之前通过js来渲染主界面 存储用户信息 将用户信息渲染在主界面上,并且头像设置跳转,到个人资料界面 这里数据库中还没有设置相关信息 2.模糊查找 检测输入框是否有变更,有的话调用方法,进行查找 发送检测请求,然后接收的时候设置最多显示四个类似的搜索结果

动手学深度学习【数据操作+数据预处理】

import osos.makedirs(os.path.join('.', 'data'), exist_ok=True)data_file = os.path.join('.', 'data', 'house_tiny.csv')with open(data_file, 'w') as f:f.write('NumRooms,Alley,Price\n') # 列名f.write('NA

线程的四种操作

所属专栏:Java学习        1. 线程的开启 start和run的区别: run:描述了线程要执行的任务,也可以称为线程的入口 start:调用系统函数,真正的在系统内核中创建线程(创建PCB,加入到链表中),此处的start会根据不同的系统,分别调用不同的api,创建好之后的线程,再单独去执行run(所以说,start的本质是调用系统api,系统的api