学习java—第三十天学习笔记

744 阅读3分钟

2019.8.15 NIIT第三十天

/第三十天了,还有3个月/

使用BeanUtils注入数据

BeanUtils工具常用工具类有两个:BeanUtils、ConvertUtils。BeanUtils用于封装数据,ConvertUtils用于处理类型转换 常用方法:

方法名 备注
setProperty(Object obj,String name,Object value) 设置属性值,如果指定属性不存在,不抛异常
getProperty(Object obj,String name) 获得属性值,如果指定属性不存在,抛方法找不到异常

方法二:BeanUtils的populate方法的使用 方法说明 BeanUtils类的populate(Object bean, Map<String,String[]> properties): 将Map数据封装到指定Javabean中,一般用于将表单的所有数据封装到javabean。

package com.igeek;

import org.apache.commons.beanutils.BeanUtils;

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

public class Test {
    public static void main(String[] args) {
//        User u=new User();
//        u.setUid("u001");
//        u.setName("jack");
//        u.setPwd("123456");
//        System.out.println(u);
        User u=new User();
        try {

            BeanUtils.setProperty(u,"uid","u002");
            System.out.println(u);
            
            BeanUtils.setProperty(u,"name","xuning");
            BeanUtils.setProperty(u,"pwd","123456");
            BeanUtils.setProperty(u,"hobbies", new String[]{"唱", "跳", "rap"});

            String s=BeanUtils.getProperty(u,"name");
            System.out.println(s);

            String s1=BeanUtils.getProperty(u,"hobbies");
            System.out.println(s1);

            String[] srr=BeanUtils.getArrayProperty(u,"hobbies");
            System.out.println(Arrays.toString(srr));

            System.out.println(u);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

自定义封装工具类

package com.igeek1;

import org.apache.commons.beanutils.BeanUtils;

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

public class MyBeanUtils {
    //封装异常
    public static void populate(Object obj, Map<String,String[]> properties){
        try {
            BeanUtils.populate(obj,properties);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    //封装对象
    public static Object populate(Class clazz,Map<String,String[]>properties){

        Object bean= null;
        try {
            bean = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        populate(bean,properties);
        return bean;
    }
    //泛型Class<T>
    public static <T> T populateT(Class<T> clazz,Map<String,String[]>properties) {

        T bean = null;
        try {
            bean = clazz.newInstance();
            populate(bean, properties);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return bean;
    }
    
}

XML

  • XML概述 XML全称为Extensible Markup Language,意思是可扩展的标记语言。 W3C在1998年2月发布1.0版本,2004年2月又发布1.1版本,但因为1.1版本不能向下兼容1.0版本,所以1.1没有人用。同时,在2004年2月W3C又发布了1.0版本的第三版。我们要学习的还是1.0版本!!! 标记,就是标签。例如:
  • XML的作用
  • 存放数据

XML申明

@Test
public void test(){
	HashSet<Bean> beans = new HashSet<Bean>();

	Bean b1 = new Bean();
	b1.setId("user1");
	b1.setClassName("com.igeek_01.User");
	Properties prop = new Properties();
	prop.setProperties("id","u001");
	prop.setProperties("username","jack");
	prop.setProperties("pwd","1234");
	b1.setProp( prop );

	Bean b2 = new Bean();
	b2.setId("user2");
	b2.setClassName("com.igeek_01.User");
	Properties prop = new Properties();
	prop.setProperties("id","u002");
	prop.setProperties("username","rose");
	prop.setProperties("pwd","5678");
	b2.setProp( prop );

	beans.add(b1);
	beans.add(b2);
}

  • 固定的文档声明
  • 合格的元素和属性
  • 正确的注释
  • 符合要求的特殊字符编写规则
  • 文件扩展名必须为xml

XML文档声明格式:

  1. 文档声明必须为结束;
  2. 文档声明必须从文档的0行0列位置开始;
  3. 文档声明只有2个属性: a) versioin:指定XML文档版本。必须属性,因为我们不会选择1.1,只会选择1.0; b) encoding:指定当前文档的编码。可选属性,常用值是utf-8;
<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<bean id="xx" className="xx">
		<property name="xx" value="xx"></property>
	</bean>
	<bean></bean>
</beans>

转义字符

因为很多符号已经被XML文档结构所使用,所以在元素体或属性值中想使用这些符号就必须使用转义字符,例如:“<”、“>”、“’”、“””、“&”。

字符 预定义实体引用
< &It;
> >
" "
' '
& &

CDATA区

DTD文件的创建与使用

使用步骤:

  • 步骤1:创建bean-dtd.xml文档,并将“bean.dtd”拷贝相同目录下。

  • 步骤2:从DTD文档开始处,拷贝需要的“文档声明” 在XML中申明

  • 步骤3:完成xml内容编写

Schema概述

Schema是新的XML文档约束; Schema要比DTD强大很多,是DTD 替代者; Schema本身也是XML文档,但Schema文档的扩展名为xsd,而不是xml。 Schema 功能更强大,数据类型更完善 Schema 支持名称空间