| 類名 | 含義 | 典型屬性示例 |
WeatherBean | 天氣數據實體 | temperature, humidity, condition, city |
CityBean | 城市信息實體 | id, name, province, population |
UserBean | 用戶信息實體 | username, password, email, role |
OrderBean | 訂單實體 | orderId, userId, amount, status |
為什麼叫「Bean」?
- 「Bean」意為「豆子」,隱喻「小巧、可復用的組件」。
- 後綴
Bean、DTO、VO、Entity 常互換使用,核心都是「數據載體」。
實際開發中的角色
- 數據傳輸層(DTO):Controller ↔ Service 傳遞參數。
- 持久層實體:MyBatis/JPA 映射數據庫表。
- 視圖模型:前端模板渲染所需數據結構。
代碼示例
public class WeatherBean implements Serializable {
private String city;
private double temperature;
private String condition; // 晴/雨/雪...
// 無參構造器
public WeatherBean() {}
// Getter/Setter
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
// ... 其他屬性同理
}
總結
WeatherBean、CityBean 等只是**業務語義下的具體 Bean 類**,並非固定類庫。開發者根據業務領域自行定義,名字通常為「業務名詞 + Bean」。
- 上一篇文章: Java FileWriter 追加寫入機制深度解析:為什麼不加 true 也能追加?
- 下一篇文章: OpenLayers 2 註冊鼠標滾輪事件的正確方法
Copyright ©
程式碼之家 All Rights Reserved