0
点赞
收藏
分享

微信扫一扫

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

卿卿如梦 2023-01-08 阅读 130

​​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。

 

当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--​​buguge​​



举报

相关推荐

0 条评论