JavaBean

概念:标准的Java类
要求:

  1. 修饰的类必须是public修饰
  2. 类的成员变量必须是private修饰
  3. 类中包含无参构造
  4. 提供公共setter和getter方法

属性:

概念:setter和getter方法截取之后的产物
如:getUsername—>Username—>username(属性)

成员变量:

概念:在类中定义的变量成为成员变量。

BeanUtils工具类

概念:主要用于封装JavaBean对象的工具类
主要方法

  1. setProperty()
  2. getProperty()
  3. populate(Object obj , Map map):将map集合的键值对信息,封装到对应的JavaBean对象中

准备实体类User:

public class User {
   
    private String username;
    private String password;
    private String gender;

    @Override
    public String toString() {
   
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
    //测试JavaBean
    public String getHehe() {
   
        return gender;
    }

    public void setHehe(String gender) {
   
        this.gender = gender;
    }

    public String getUsername() {
   
        return username;
    }

    public void setUsername(String username) {
   
        this.username = username;
    }

    public String getPassword() {
   
        return password;
    }

    public void setPassword(String password) {
   
        this.password = password;
    }
}

(1)演示:setProperty()

    @Test
    public void test() throws Exception {
   

        User user = new User();
        BeanUtils.setProperty(user,"username","male");
        BeanUtils.setProperty(user,"gender","12");
        System.out.println(user);
        System.out.println("------设置hehe的值对应到gender------");
        BeanUtils.setProperty(user,"hehe","12");
        System.out.println(user);
    }

输出:

(2)演示:setProperty()

    @Test
    public void test() throws Exception {
   

        User user = new User();
        user.setUsername("liuzeyu12a");
        user.setHehe("44");
        String username = BeanUtils.getProperty(user, "username");
        //String gender1 = BeanUtils.getProperty(user, "gender");  报错:未找到属性
        //System.out.println("gender:"+gender1);
        System.out.println(username);
        System.out.println("------获取hehe的值对应到gender的值------");
        String gender2 = BeanUtils.getProperty(user, "hehe");
        System.out.println("gender:"+gender2);
    }

输出:

(3)演示:populate(Object obj , Map map)
访问项目(案例:实现登录功能 loginServlet实现):

https://blog.csdn.net/JAYU_37/article/details/104502791