首页 > 编程语言 >Java中的POJO与JavaBean / Java Bean与POJO的区别与联系

Java中的POJO与JavaBean / Java Bean与POJO的区别与联系

时间:2023-01-06 15:36:16浏览次数:73  
标签:Java long current records Bean POJO public size

POJO(Plain Ordinary Java Object)即普通Java类,具有一部分getter/setter方法的那种类就可以称作POJO。

  1. 有一些private的参数作为对象的属性,然后针对每一个参数定义get和set方法访问的接口。
  2. 没有从任何类继承、也没有实现任何接口,更没有被其它框架侵入的java对象。

 

JavaBean 是一种JAVA语言写成的可重用组件。JavaBean符合一定规范编写的Java类,不是一种技术,而是一种规范。大家针对这种规范,总结了很多开发技巧、工具函数。符合这种规范的类,可以被其它的程序或者框架使用。JavaBean的任务就是: “Write once, run anywhere, reuse everywhere”,即“一次性编写,任何地方执行,任何地方重用”。它的方法命名,构造及行为必须符合特定的约定:

  1. 所有属性为private
  2. 这个类必须有一个公共的缺省构造函数。即是提供无参数的构造器
  3. 这个类的属性使用getter和setter来访问,其他方法遵从标准命名规范
  4. 这个类应是可序列化的。实现serializable接口

 

因为这些要求主要是靠约定而不是靠实现接口,所以许多开发者把JavaBean看作遵从特定命名约定的POJO。

 

两者之间的区别

  1. POJO其实是比javabean更纯净的简单类或接口。POJO严格地遵守简单对象的概念,而一些JavaBean中往往会封装一些简单逻辑。
  2. POJO主要用于数据的临时传递,它只能装载数据, 作为数据存储的载体,而不具有业务逻辑处理的能力。
  3. Javabean虽然数据的获取与POJO一样,但是javabean当中可以有其它的方法。

 

我们的项目通常的是分层结构的,所以POJO有许多。

例如(这里用lombok的@Data注解省略了属性的getter/setter方法):

import lombok.Data;

@Data
public class User implements Serializable {
    private String id;
    private String name;
    private Integer age;
}

 

下面来看一个JavaBean的示例。对于页面分页,大家都不陌生。如下是一个分页组件示例:

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

/**
 * 分页组件
 * @author hongfei.guo
 */
public class MyPage<T> implements java.io.Serializable {

    private static final long serialVersionUID = 8545996863226528798L;

    private List<T> records;
    private long total;
    private long size;
    private long current;
    private boolean isSearchCount;

    public MyPage() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.isSearchCount = true;
    }

    public MyPage(long current, long size) {
        this(current, size, 0L);
    }

    public MyPage(long current, long size, long total) {
        this(current, size, total, true);
    }

    public MyPage(long current, long size, boolean isSearchCount) {
        this(current, size, 0L, isSearchCount);
    }

    public MyPage(long current, long size, long total, boolean isSearchCount) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.isSearchCount = true;
        if (current > 1L) {
            this.current = current;
        }

        this.size = size;
        this.total = total;
        this.isSearchCount = isSearchCount;
    }

    public <S> MyPage(MyIPage<S> sourcePage, Function<T,S> transFun) {
        this.records = Collections.emptyList();
        this.total = sourcePage.getTotal();
        this.size = sourcePage.getSize();
        this.current = sourcePage.getCurrent();
        this.isSearchCount = true;

        final List records = sourcePage.getRecords();
        if (records != null) {
            for (Object record : records) {
                transFun.apply((T) record);
            }
        }
    }

    public boolean hasPrevious() {
        return this.current > 1L;
    }

    public boolean hasNext() {
        return this.current < this.getPages();
    }

    public List<T> getRecords() {
        return this.records;
    }

    public MyPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public long getSize() {
        return this.size;
    }

    public long getCurrent() {
        return this.current;
    }

    public MyPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public boolean isSearchCount() {
        return this.total < 0L ? false : this.isSearchCount;
    }

}

另外,在DDD领域驱动设计中,领域模型都是JavaBean。

 

标签:Java,long,current,records,Bean,POJO,public,size
From: https://www.cnblogs.com/buguge/p/17030589.html

相关文章

  • Java final关键字修饰对象
    结论:final修饰对象,对象的内容可变,引用不可变。final应用于类、方法和变量、对象时意义是不同的,但本质是一样的,都表示不可改变。但是修饰对象时要注意:因为对象是引用类......
  • JAVA中的定时器使用
    1、注解作用@Configuration//主要用于标记配置类,兼备Component的效果。@EnableScheduling//开启定时任务@Scheduled(cron="0/5****?")//添加定时......
  • javashop配置微信支付
    1、登录后台->找到设置->配送和支付->支付方式2、此处需要配置:卖家微信账户id、支付appid、微信安全校验码、密钥1)卖家微信账户id:就是pay.weixin.qq.com,微信商户中心里面的......
  • java使用Post方式发送https请求的方法,直接可以用
                  踩过无数坑之后,成功的方案,主要在设置Content-type application/x-www-form-urlencoded这里,之前没设置,一直数据不通过,不过好了现在OK了  ......
  • JAVA利用google的zxing快速生成QRCode
    利用google的zxing快速生成QRCode1.导入jar包,如果是非maven工程就去mvnrepository.com搜索zxing,下载本jar包即可<dependency><groupId>com.google.zxing</groupId><......
  • 李荣先辈Java简介
     李荣先辈是国内Java的最早的一批开发人员。其代表作有《逸一时误一世Java》和《逸久逸久罢一龄Java》等,每部作品的深度都给后来学Java的同学带来巨大的帮助;......
  • java多线程创建一个简单的案例
    1、创建一个简单的线程,不需要去创建个RunnableThreadthread=newThread(newRunnable(){@Overridepublicvoidrun(){//todo你要执行的方法}......
  • windows下gradle下使用windows版普罗米修斯prometheus和metrics简单的制作一个监控jav
    1.引入依赖(如果是maven可以自行翻译):例如:compile'io.prometheus:simpleclient_hotspot:0.5.0'groupid为io.prometheusartifactid为......
  • java懒人操作CURT
    1、java对象复制:BeanUtils.copyProperties(temp,reimbursement);2、流操作List对象转map:Map<String,Fields>fieldsMap=fields.stream().collect(Collectors.toMap(F......
  • AJAX跨域请求的理解,JAVA
    1.浏览器的同源策略  目前所有浏览器都由同源策略      什么是同源策略:   协议、域名、端口都一直的uri称为“同源”       不同源之间存在以下......