在之前的博文中,我们初步制作了界面,但是还是有很多问题有待解决,那么,在这篇博文中,本人来讲解model层的设计。在代码展示的过程中,相信同学们会明白本人建立这个层的原因。在代码展示完后还是对于这个概念感觉迷茫的同学不用心急,本人将在这篇博文的代码展示后,进行讲解。
首先,现在,本人在来给出一个存储学生所有信息的model:
package com.mec.mis.model;
public class StudentDetailModel {
private String id;
private String stuId;
private String name;
private NativeModel nativer;
private NationModel national;
private SDMModel sdm;
private boolean status;
public StudentDetailModel() {
}
public StudentDetailModel(String id, String stuId, String name,
NativeModel nativer, NationModel national,
SDMModel sdm, boolean status) {
this.id = id;
this.stuId = stuId;
this.name = name;
this.nativer = nativer;
this.national = national;
this.sdm = sdm;
this.status = status;
}
public String getId() {
return id;
}
public StudentDetailModel setId(String id) {
this.id = id;
return this;
}
public String getStuId() {
return stuId;
}
public StudentDetailModel setStuId(String stuId) {
this.stuId = stuId;
return this;
}
public String getName() {
return name;
}
public StudentDetailModel setName(String name) {
this.name = name;
return this;
}
public NativeModel getNativer() {
return nativer;
}
public StudentDetailModel setNativer(NativeModel nativer) {
this.nativer = nativer;
return this;
}
public NationModel getNational() {
return national;
}
public StudentDetailModel setNational(NationModel national) {
this.national = national;
return this;
}
public SDMModel getSdm() {
return sdm;
}
public StudentDetailModel setSdm(SDMModel sdm) {
this.sdm = sdm;
return this;
}
public boolean isStatus() {
return status;
}
public StudentDetailModel setStatus(boolean status) {
this.status = status;
return this;
}
@Override
public String toString() {
return stuId + " " + id + " " + name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((stuId == null) ? 0 : stuId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StudentDetailModel other = (StudentDetailModel) obj;
if (stuId == null) {
if (other.stuId != null)
return false;
} else if (!stuId.equals(other.stuId))
return false;
return true;
}
}
上面的代码的主要功能还是很简单,只是单纯地对学生信息进行Getter 和 Setter。
以及最后的比较方法的覆盖——按照stuId比较
现在,本人先来展示一个用于存储学生信息的 model:
package com.mec.mis.model;
public class SDMModel {
private String id;
private String name;
private boolean status;
public SDMModel() {
}
public SDMModel(String id, String name, boolean status) {
this.id = id;
this.name = name;
this.status = status;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public boolean isStatus() {
return status;
}
public SDMModel setId(String id) {
this.id = id;
return this;
}
public SDMModel setName(String name) {
this.name = name;
return this;
}
public SDMModel setStatus(boolean status) {
this.status = status;
return this;
}
@Override
public String toString() {
return id + (status ? " √ " : " × ") + name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (status ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SDMModel other = (SDMModel) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (status != other.status)
return false;
return true;
}
}
可以看到,上述代码的逻辑结构很简单,只是对于学生的学号、姓名、状态的简单的Getter 和 Setter,并且,在最后覆盖了比较方法——通过id比较。
那么,现在,本人再来展示一个用于存储民族信息的类:
package com.mec.mis.model;
public class NationModel {
private String id;
private String name;
public NationModel() {
}
public NationModel(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public NationModel setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public NationModel setName(String name) {
this.name = name;
return this;
}
@Override
public String toString() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NationModel other = (NationModel) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
这个类和本人之后给出的一个关于存储省份信息的类的功能都是为了我们之后通过读取数据库的信息,处理下拉菜单用的。
那现在本人来给出一个用于存储省份信息的类:
package com.mec.mis.model;
public class NativeModel {
private String id;
private String name;
public NativeModel() {
}
public NativeModel(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public NativeModel setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public NativeModel setName(String name) {
this.name = name;
return this;
}
@Override
public String toString() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NativeModel other = (NativeModel) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
现在,本人给出一个用来存储学生信息和数据库中的表的对应关系 的model:
package com.mec.mis.model;
public class StudentDetailModel {
private String id;
private String stuId;
private String name;
private NativeModel nativer;
private NationModel national;
private SDMModel sdm;
private boolean status;
public StudentDetailModel() {
}
public StudentDetailModel(String id, String stuId, String name,
NativeModel nativer, NationModel national,
SDMModel sdm, boolean status) {
this.id = id;
this.stuId = stuId;
this.name = name;
this.nativer = nativer;
this.national = national;
this.sdm = sdm;
this.status = status;
}
public String getId() {
return id;
}
public StudentDetailModel setId(String id) {
this.id = id;
return this;
}
public String getStuId() {
return stuId;
}
public StudentDetailModel setStuId(String stuId) {
this.stuId = stuId;
return this;
}
public String getName() {
return name;
}
public StudentDetailModel setName(String name) {
this.name = name;
return this;
}
public NativeModel getNativer() {
return nativer;
}
public StudentDetailModel setNativer(NativeModel nativer) {
this.nativer = nativer;
return this;
}
public NationModel getNational() {
return national;
}
public StudentDetailModel setNational(NationModel national) {
this.national = national;
return this;
}
public SDMModel getSdm() {
return sdm;
}
public StudentDetailModel setSdm(SDMModel sdm) {
this.sdm = sdm;
return this;
}
public boolean isStatus() {
return status;
}
public StudentDetailModel setStatus(boolean status) {
this.status = status;
return this;
}
@Override
public String toString() {
return stuId + " " + id + " " + name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((stuId == null) ? 0 : stuId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StudentDetailModel other = (StudentDetailModel) obj;
if (stuId == null) {
if (other.stuId != null)
return false;
} else if (!stuId.equals(other.stuId))
return false;
return true;
}
}
本人在这里再进行一次说明:
1.第一个model,是我们用于存储到窗口的列表区后,读取时用的;
2.剩下的model,是我们为了根据数据库内的信息,处理下拉列表时用的。
那么,若是本人觉得,讲解对于看完后的同学而言,还是蛮清楚的,所以,希望同学们在看到本人的代码,觉得有一些是画蛇添足的,请稍加注意,本人将在本专题之后的几篇博文中展现本人为什么给出那些“画蛇添足”的代码段。
那么,我们本专题想要制作的 model层 就做出来了,至于接下来的几步操作,将会在本人本专题之后的几篇博文中进行讲解!