JavaWeb-简析JavaBean

9.JavaBean

我们通常叫JavaBean为:实体类。

JavaBean有其特定的写法:

  • 必须有一个无参构造。

  • 属性必须私有化

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

一般用来和数据库的字段作映射——>ORM;

ORM:对象关系映射。

  • 表——>类

  • 字段——>属性

  • 行记录——>对象

举例:

people表格:

create database Jsp;

use Jsp;

create table people(
	`id` int(7) not null primary key,
	`name` varchar(37) not null,
	`age` int(3) not null,
	`address` varchar(77) not null
)Engine = InnoDB default charset = utf8;
id name age address
1 Edwin 17 海口
2 Jarvis 27 海口
3 Rui 7 海口

对应到Java中:

class Poeple{
    private int id;
    private String name;
    private int age;
    private String address;
}

class Add{
    new People(1,"Edwin",17,"海口");
    new People(2,"Jarvis",27,"海口");
    new People(3,"Rui",7,"海口");
}

实际操作:

People类:

package com.edwin.pojo;
/**
 * @author Edwin D
 * @date 2020.6.9 下午 7:03
 */
//实体类我们一般都是和数据库中的表一一对应的。
public class People {
    private int id;
    private String name;
    private int age;
    private String address;

    public People() {
    }

    public People(int id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}

JavaBean.jsp

<%@ page import="com.edwin.pojo.People" %><%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.9
  Time: 下午 7:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JavaBean</title>
</head>
<body>

<%
//    People people = new People();
//    people.setId(1);
//    people.setName("Edwin Duan");
//    people.setAge(177);
//    people.setAddress("海口");
%>
<%--ID:<%=people.getId()%>--%>
<%--姓名:<%=people.getName())%>--%>
<%--年龄:<%=people.getAge()%>--%>
<%--地址:<%=people.getAddress()%>--%>

<%--上下等价。--%>

<%--新建一个对象--%>
<jsp:useBean id="people" class="com.edwin.pojo.People" scope="page"/>
<%--赋值--%>
<jsp:setProperty name="people" property="id" value="1"/>
<jsp:setProperty name="people" property="name" value="Edwin 段"/>
<jsp:setProperty name="people" property="age" value="17"/>
<jsp:setProperty name="people" property="address" value="海口"/>
<%--取值--%>
ID:<jsp:getProperty name="people" property="id"/>
姓名:<jsp:getProperty name="people" property="name"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
</body>
</html>

输出效果:

在这里插入图片描述

《成功的花》——冰心
成功的花,
人们只惊羡她现时的明艳!
然而当初她的芽儿,
浸透了奋斗的泪泉,
洒遍了牺牲的血雨!

参考文献

《【狂神说Java】JavaWeb入门到实战》

视频连接

2020.06.16

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章