TCP服务器和客户端学生管理系统(第一天)
一,工具类(util包中)第二天有补充
1.1 资源管理close工具类
package com.qfedu.student.system.client.util;
import java.io.Closeable;
import java.io.IOException;
/**
* 资源管理Close工具类
*
* @author Anonymous 2020/3/17 14:33
*/
public class CloseUtil {
/**
* 可以调用close方法的类都可以使用
*
* @param res 不定长参数Closeable接口实现类,处理资源关闭问题
*/
public static void closeAll(Closeable... res) {
for (Closeable re : res) {
try {
if (re != null) {
re.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2 Json数据格式操作工具类
package com.qfedu.student.system.client.util;
import com.alibaba.fastjson.JSON;
import com.qfedu.student.system.client.entity.Student;
import java.util.List;
/**
* Json数据格式操作工具类
*
*
* 服务器和客户端直接的数据交互方式
* 3. 解析Json格式数据 ==> Object
* 4. Object ==> 转换为Json格式数据
* 5. 解析Json格式数据 ==> List<Object>
* 6. List<Object> ==> 转换为Json格式数据
*
* @author Anonymous 2020/3/17 11:31
*/
public class JsonUtil {
/**
* Json字符串转换成对应Student类对象
*
* @param jsonString Json格式字符串
* @return Student类对象
*/
public static Student jsonToStudent(String jsonString) {
return JSON.parseObject(jsonString, Student.class);
}
/**
* Student类对象转换成Json格式字符串
*
* @param student Student类对象数据
* @return 符合Json格式的String类型字符串
*/
public static String studentToJson(Student student) {
return JSON.toJSONString(student);
}
/**
* Json格式字符串转换成保存Student类型的List集合
*
* @param jsonString Json格式字符串
* @return 保存Student类型List集合
*/
public static List<Student> jsonToStudentList(String jsonString) {
return JSON.parseArray(jsonString, Student.class);
}
/**
* 保存Student类型的List集合转换成一个Json格式字符串
*
* @param list 保存Student类型的List集合
* @return Json格式字符串
*/
public static String studentListToJson(List<Student> list) {
return JSON.toJSONString(list);
}
}
二,Student实体类(entity包中)
严格按照JavaBean规范实现
1. 所有成员变量全部私有化
2. 必须提供一个无参数构造方法
3. 完成对应成员变量的setter和getter
4. 这里建议使用【包装类】
代码演示
package com.qfedu.student.system.client.entity;
/**
* Student实体类,要求符合JavaBean规范
* 对象关系映射(Object Relational Mapping) ORM
* Object Java中的任何一个对象 保存映射到对应的 数据保存 ==> 文件 XML JSON Database
*
* @author Anonymous 2020/3/17 10:00
*/
public class Student {
private Integer id;
private String name;
private Boolean gender;
private Integer age;
private Integer mathScore;
private Integer chnScore;
private Integer engScore;
private Integer totalScore;
private Integer rank;
public Student() {
}
public Student(Integer id, String name, Boolean gender, Integer age, Integer mathScore, Integer chnScore, Integer engScore, Integer totalScore, Integer rank) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.mathScore = mathScore;
this.chnScore = chnScore;
this.engScore = engScore;
this.totalScore = totalScore;
this.rank = rank;
}