2022.5.11 JavaBean

9、JavaBean

实体类 JavaBean有特定的写法:

  • 必须要有一一个无参构造

  • 属性必须私有化

  • 必须有对应的get/set方法;

一般用来和数据库的字段做映射ORM;

ORM :对象关系映射

  • 表-->类

  • 字段-->属性

  • 行记录--->对象

 

 

 

 package com.xing.pojo;
 ​
 //一个实体类对应数据库的一张表
 public class Person {
     private String name;
     private int age;
     private int id;
     private String address;
 ​
     public Person() {
    }
 ​
     public Person(String name, int age, int id, String address) {
         this.name = name;
         this.age = age;
         this.id = id;
         this.address = address;
    }
 ​
     public String getName() {
         return name;
    }
 ​
     public void setName(String name) {
         this.name = name;
    }
 ​
     public int getAge() {
         return age;
    }
 ​
     public void setAge(int age) {
         this.age = age;
    }
 ​
     public int getId() {
         return id;
    }
 ​
     public void setId(int id) {
         this.id = id;
    }
 ​
     public String getAddress() {
         return address;
    }
 ​
     public void setAddress(String address) {
         this.address = address;
    }
 ​
     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 ", id=" + id +
                 ", address='" + address + '\'' +
                 '}';
    }
 }
 ​

 

 

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <html>
 <head>
     <title>Title</title>
 </head>
 <body>
 <%-- 相当于 Person p = new Person()  id为类的实例化对象名  scope作用域:这个页面   property:这个类的变量--%>
 <jsp:useBean id="p" class="com.xing.pojo.Person" scope="page">
     <jsp:setProperty name="p" property="id" value="3"/>
     <jsp:setProperty name="p" property="name" value="小明"/>
     <jsp:setProperty name="p" property="age" value="30"/>
     <jsp:setProperty name="p" property="address" value="北京"/>
 </jsp:useBean>
 ​
 ​
 姓名: <jsp:getProperty name="p" property="name"/>
 id:   <jsp:getProperty name="p" property="id"/>
 年龄: <jsp:getProperty name="p" property="age"/>
 地址: <jsp:getProperty name="p" property="address"/>
 ​
 </body>
 </html>
 ​
 

 

 

posted @ 2022-05-23 09:12  暴躁C语言  阅读(34)  评论(0编辑  收藏  举报