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

相关文章

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

Java Stream.reduce()方法操作实际案例讲解

《JavaStream.reduce()方法操作实际案例讲解》reduce是JavaStreamAPI中的一个核心操作,用于将流中的元素组合起来产生单个结果,:本文主要介绍JavaStream.... 目录一、reduce的基本概念1. 什么是reduce操作2. reduce方法的三种形式二、reduce