Add JavaBean and DatabasePool

This commit is contained in:
2022-06-06 18:42:31 +08:00
parent f1b8facc03
commit bef2711600
27 changed files with 1276 additions and 0 deletions

View File

@@ -11,5 +11,8 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="log4j-core-2.17.2" level="project" />
<orderEntry type="library" name="log4j-api-2.17.2" level="project" />
<orderEntry type="library" name="zaxxer.HikariCP" level="project" />
<orderEntry type="library" name="mysql-connector-java-8.0.25" level="project" />
<orderEntry type="library" name="slf4j.simple" level="project" />
</component>
</module>

View File

@@ -0,0 +1,76 @@
package com.cfive.classroom.library.database;
import com.cfive.classroom.library.database.util.NoConfigException;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class PoolHelper {
private static final Logger LOGGER = LogManager.getLogger();
private static HikariDataSource hikariDataSource;
private static String JDBC_URL = "jdbc:mysql://%s:%s/%s?useSSL=false&characterEncoding=utf8";
private static String JDBC_USERNAME;
private static String JDBC_PASSWORD;
private static final String CACHE_PREP_STMTS = "true";
private static final String PREP_STMT_CACHE_SIZE = "100";
private static final String MAXIMUM_POOL_SIZE = "10";
public static void init() throws NoConfigException {
loadProperties();
HikariConfig config = new HikariConfig();
config.setJdbcUrl(JDBC_URL);
config.setUsername(JDBC_USERNAME);
config.setPassword(JDBC_PASSWORD);
config.addDataSourceProperty("cachePrepStmts", CACHE_PREP_STMTS);
config.addDataSourceProperty("prepStmtCacheSize", PREP_STMT_CACHE_SIZE);
config.addDataSourceProperty("maximumPoolSize", MAXIMUM_POOL_SIZE);
hikariDataSource = new HikariDataSource(config);
}
private static void loadProperties() throws NoConfigException {
Properties properties = new Properties();
try {
properties.load(new FileReader("mysql.properties"));
if (properties.getProperty("ip").isEmpty() || properties.getProperty("port").isEmpty() || properties.getProperty("database").isEmpty() || properties.getProperty("username").isEmpty() || properties.getProperty("password").isEmpty()) {
LOGGER.error("配置有误,请重新配置或删除配置文件重新生成");
throw new NoConfigException();
} else {
JDBC_URL = String.format(JDBC_URL, properties.getProperty("ip"), properties.getProperty("port"), properties.getProperty("database"));
JDBC_USERNAME = properties.getProperty("username");
JDBC_PASSWORD = properties.getProperty("password");
}
} catch (IOException e) {
properties.setProperty("ip", "");
properties.setProperty("port", "");
properties.setProperty("database", "");
properties.setProperty("username", "");
properties.setProperty("password", "");
try {
properties.store(new FileWriter("mysql.properties"), "MySQL Configuration");
LOGGER.info("已创建配置文件“mysql.properties”");
} catch (IOException ioException) {
LOGGER.error("无法创建配置文件“mysql.properties”,请手动配置:创建配置文件并添加以下内容\nip=[数据库IP]\nport=[数据库端口]\ndatabase=[数据库名]\nusername=[数据用户名]\npassword=[数据库密码]", e);
}
throw new NoConfigException();
}
}
public static void close() {
hikariDataSource.close();
}
public static Connection getConnection() throws SQLException, NoConfigException {
if (hikariDataSource == null) {
init();
}
return hikariDataSource.getConnection();
}
}

View File

@@ -0,0 +1,22 @@
package com.cfive.classroom.library.database.bean;
/**
* 考勤状态枚举类
*
* @author FatttSnake
* @version 1.0
*/
public enum AttStatus {
not_signed("未签"), signed("已签"), absence("缺勤"), personal_leave("事假"), sick_leave("病假"), public_holiday("公假"), late("迟到"), leave_early("早退");
private final String string;
AttStatus(String string) {
this.string = string;
}
@Override
public String toString() {
return string;
}
}

View File

@@ -0,0 +1,137 @@
package com.cfive.classroom.library.database.bean;
import java.time.LocalDateTime;
/**
* 考勤类,用于考勤信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Attendance {
private final String attID;
private Course course;
private Student student;
private LocalDateTime attTime;
private AttStatus attStatus;
/**
* 构造考勤对象
*
* @param attID 考勤编号,用于识别唯一考勤
* @param course 隶属课程
* @param student 隶属学生
* @param attTime 考勤时间
* @param attStatus 考勤状态
*/
public Attendance(String attID, Course course, Student student, LocalDateTime attTime, AttStatus attStatus) {
this.attID = attID;
this.course = course;
this.student = student;
this.attTime = attTime;
this.attStatus = attStatus;
}
/**
* 获取考勤编号
*
* @return <code>String</code> 考勤编号UUID字符串
*/
public String getAttID() {
return attID;
}
/**
* 获取隶属课程
*
* @return <code>Course</code> 隶属课程
* @see Course
*/
public Course getCourse() {
return course;
}
/**
* 设置隶属课程
*
* @param course 隶属课程
* @see Course
*/
public void setCourse(Course course) {
this.course = course;
}
/**
* 获取隶属学生
*
* @return <code>Student</code> 隶属学生
* @see Student
*/
public Student getStudent() {
return student;
}
/**
* 设置隶属学生
*
* @param student 隶属学生
* @see Student
*/
public void setStudent(Student student) {
this.student = student;
}
/**
* 获取考勤时间
*
* @return <code>LocalDateTime</code> 考勤时间
*/
public LocalDateTime getAttTime() {
return attTime;
}
/**
* 设置考勤时间
*
* @param attTime 考勤时间
*/
public void setAttTime(LocalDateTime attTime) {
this.attTime = attTime;
}
/**
* 获取考勤状态
*
* @return <code>AttStatus</code> 考勤状态
* @see AttStatus
*/
public AttStatus getAttStatus() {
return attStatus;
}
/**
* 设置考勤状态
*
* @param attStatus 考勤状态
* @see AttStatus
*/
public void setAttStatus(AttStatus attStatus) {
this.attStatus = attStatus;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Attendance{" +
"attID='" + attID + '\'' +
", course=" + course +
", student=" + student +
", attTime=" + attTime +
", attStatus=" + attStatus +
'}';
}
}

View File

@@ -0,0 +1,112 @@
package com.cfive.classroom.library.database.bean;
import com.sun.istack.internal.NotNull;
/**
* 班级类,用于班级信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Class {
private final long classID;
private Major major;
private int grade;
private int classNum;
/**
* 构造班级对象
*
* @param classID 班级编号,用于识别唯一班级
* @param major 隶属专业
* @param grade 年级
* @param classNum 班号
*/
public Class(@NotNull long classID, @NotNull Major major, @NotNull int grade, @NotNull int classNum) {
this.classID = classID;
this.major = major;
this.grade = grade;
this.classNum = classNum;
}
/**
* 获取班级编号,用于识别唯一班级
*
* @return <code>long</code> 班级编号
*/
public long getClassID() {
return classID;
}
/**
* 获取隶属专业
*
* @return <code>Major</code> 隶属专业
* @see Major
*/
public Major getMajor() {
return major;
}
/**
* 设置隶属专业
*
* @param major 隶属专业
* @see Major
*/
public void setMajor(@NotNull Major major) {
this.major = major;
}
/**
* 获取年级
*
* @return <code>int</code> 年级
*/
public int getGrade() {
return grade;
}
/**
* 设置年级
*
* @param grade 年级
*/
public void setGrade(@NotNull int grade) {
this.grade = grade;
}
/**
* 获取班号
*
* @return <code>int</code> 班号
*/
public int getClassNum() {
return classNum;
}
/**
* 设置班号
*
* @param classNum 班号
*/
public void setClassNum(@NotNull int classNum) {
this.classNum = classNum;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Class{" +
"classID=" + classID +
", major=" + major +
", grade=" + grade +
", classNum=" + classNum +
'}';
}
}

View File

@@ -0,0 +1,139 @@
package com.cfive.classroom.library.database.bean;
import java.time.LocalDateTime;
/**
* 课程类,用于课程信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Course {
private final long courID;
private Subject subject;
private Teacher teacher;
private LocalDateTime courTimeFrom;
private LocalDateTime courTimeEnd;
/**
* 构造课程对象
*
* @param courID 课程编号,用于识别唯一课程
* @param subject 隶属科目
* @param teacher 隶属教师
* @param courTimeFrom 开始时间
* @param courTimeEnd 结束时间
*/
public Course(long courID, Subject subject, Teacher teacher, LocalDateTime courTimeFrom, LocalDateTime courTimeEnd) {
this.courID = courID;
this.subject = subject;
this.teacher = teacher;
this.courTimeFrom = courTimeFrom;
this.courTimeEnd = courTimeEnd;
}
/**
* 获取课程编号,用于识别唯一课程
*
* @return <code>long</code> 课程编号
*/
public long getCourID() {
return courID;
}
/**
* 获取隶属科目
*
* @return <code>Subject</code> 隶属科目
* @see Subject
*/
public Subject getSubject() {
return subject;
}
/**
* 设置隶属科目
*
* @param subject 隶属科目
* @see Subject
*/
public void setSubject(Subject subject) {
this.subject = subject;
}
/**
* 获取隶属教师
*
* @return <code>Teacher</code> 隶属教师
* @see Teacher
*/
public Teacher getTeacher() {
return teacher;
}
/**
* 设置隶属教师
*
* @param teacher 隶属教师
* @see Teacher
*/
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
/**
* 获取开始时间
*
* @return <code>LocalDateTime</code> 开始时间
* @see LocalDateTime
*/
public LocalDateTime getCourTimeFrom() {
return courTimeFrom;
}
/**
* 设置开始时间
*
* @param courTimeFrom 开始时间
* @see LocalDateTime
*/
public void setCourTimeFrom(LocalDateTime courTimeFrom) {
this.courTimeFrom = courTimeFrom;
}
/**
* 获取结束时间
*
* @return <code>LocalDateTime</code> 结束时间
* @see LocalDateTime
*/
public LocalDateTime getCourTimeEnd() {
return courTimeEnd;
}
/**
* 设置结束时间
*
* @param courTimeEnd 结束时间
* @see LocalDateTime
*/
public void setCourTimeEnd(LocalDateTime courTimeEnd) {
this.courTimeEnd = courTimeEnd;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Course{" +
"courID=" + courID +
", subject=" + subject +
", teacher=" + teacher +
", courTimeFrom=" + courTimeFrom +
", courTimeEnd=" + courTimeEnd +
'}';
}
}

View File

@@ -0,0 +1,65 @@
package com.cfive.classroom.library.database.bean;
import com.sun.istack.internal.NotNull;
/**
* 院系类,用于院系信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Faculty {
private final int facID;
private String facName;
/**
* 构造院系对象
*
* @param facID 院系编号,用于识别唯一院系
* @param facName 院系名
*/
public Faculty(@NotNull int facID, @NotNull String facName) {
this.facID = facID;
this.facName = facName;
}
/**
* 获取院系编号,用于识别唯一院系
*
* @return 院系编号
*/
public int getFacID() {
return facID;
}
/**
* 获取院系名
*
* @return 院系名
*/
public String getFacName() {
return facName;
}
/**
* 设置院系名
*
* @param facName <code>String</code> 院系名
*/
public void setFacName(@NotNull String facName) {
this.facName = facName;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Faculty{" +
"facID=" + facID +
", facName='" + facName + '\'' +
'}';
}
}

View File

@@ -0,0 +1,27 @@
package com.cfive.classroom.library.database.bean;
/**
* 性别枚举类
*
* @author FatttSnake
* @version 1.0
*/
public enum Gender {
m(""), f("");
private final String string;
Gender(String string) {
this.string = string;
}
/**
* 转换成中文
*
* @return <code>String</code> 中文
*/
@Override
public String toString() {
return string;
}
}

View File

@@ -0,0 +1,89 @@
package com.cfive.classroom.library.database.bean;
import com.sun.istack.internal.NotNull;
/**
* 专业类,用于专业信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Major {
private final int majorID;
private String majorName;
private Faculty faculty;
/**
* 构造专业对象
*
* @param majorID 专业编号,用于识别唯一专业
* @param majorName 专业名
* @param faculty 隶属院系
*/
public Major(@NotNull int majorID, @NotNull String majorName, @NotNull Faculty faculty) {
this.majorID = majorID;
this.majorName = majorName;
this.faculty = faculty;
}
/**
* 获取专业编号,用于识别唯一专业
*
* @return <code>int</code> 专业编号
*/
public int getMajorID() {
return majorID;
}
/**
* 获取专业名
*
* @return <code>String</code> 专业名
*/
public String getMajorName() {
return majorName;
}
/**
* 设置专业名
*
* @param majorName 专业名
*/
public void setMajorName(@NotNull String majorName) {
this.majorName = majorName;
}
/**
* 获取隶属院系
*
* @return <code>Faculty</code> 隶属院系
* @see Faculty
*/
public Faculty getFaculty() {
return faculty;
}
/**
* 设置隶属院系
*
* @param faculty 隶属院系
* @see Faculty
*/
public void setFaculty(@NotNull Faculty faculty) {
this.faculty = faculty;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Major{" +
"majorID=" + majorID +
", majorName='" + majorName + '\'' +
", faculty=" + faculty +
'}';
}
}

View File

@@ -0,0 +1,157 @@
package com.cfive.classroom.library.database.bean;
import com.sun.istack.internal.NotNull;
/**
* 学生类,用于学生信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Student {
private final long stuID;
private String stuName;
private Gender gender;
private Class aClass;
private String password;
private String salt;
/**
* 构造学生对象
*
* @param stuID 学号,用于识别唯一学生
* @param stuName 学生姓名
* @param gender 性别
* @param aClass 隶属班级
* @param password 密码
* @param salt 加盐
*/
public Student(@NotNull long stuID, @NotNull String stuName, @NotNull Gender gender, @NotNull Class aClass, @NotNull String password, @NotNull String salt) {
this.stuID = stuID;
this.stuName = stuName;
this.gender = gender;
this.aClass = aClass;
this.password = password;
this.salt = salt;
}
/**
* 获取学生编号,用于识别唯一学生
*
* @return <code>long</code> 学号
*/
public long getStuID() {
return stuID;
}
/**
* 获取学生姓名
*
* @return <code>String</code> 学生姓名
*/
public String getStuName() {
return stuName;
}
/**
* 设置学生姓名
*
* @param stuName 学生姓名
*/
public void setStuName(@NotNull String stuName) {
this.stuName = stuName;
}
/**
* 获取性别
*
* @return <code>Gender</code> 性别
* @see Gender
*/
public Gender getGender() {
return gender;
}
/**
* 设置性别
*
* @param gender 性别
* @see Gender
*/
public void setGender(@NotNull Gender gender) {
this.gender = gender;
}
/**
* 获取隶属班级
*
* @return <code>Class</code> 隶属班级
* @see Class
*/
public Class getaClass() {
return aClass;
}
/**
* 设置隶属班级
*
* @param aClass 隶属班级
* @see Class
*/
public void setaClass(@NotNull Class aClass) {
this.aClass = aClass;
}
/**
* 获取密码
*
* @return <code>String</code> 密码
*/
public String getPassword() {
return password;
}
/**
* 设置密码
*
* @param password 密码
*/
public void setPassword(@NotNull String password) {
this.password = password;
}
/**
* 获取加盐
*
* @return <code>String</code> 加盐
*/
public String getSalt() {
return salt;
}
/**
* 设置加盐
*
* @param salt 加盐
*/
public void setSalt(@NotNull String salt) {
this.salt = salt;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Student{" +
"stuID=" + stuID +
", stuName='" + stuName + '\'' +
", gender=" + gender +
", aClass=" + aClass +
", password='" + password + '\'' +
", salt='" + salt + '\'' +
'}';
}
}

View File

@@ -0,0 +1,65 @@
package com.cfive.classroom.library.database.bean;
import com.sun.istack.internal.NotNull;
/**
* 科目类,用于科目信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Subject {
private final int subID;
private String subName;
/**
* 构造科目对象
*
* @param subID 科目编号,用于识别唯一科目
* @param subName 科目名
*/
public Subject(@NotNull int subID, @NotNull String subName) {
this.subID = subID;
this.subName = subName;
}
/**
* 获取科目编号,用于识别唯一科目
*
* @return <code>int</code> 科目编号
*/
public int getSubID() {
return subID;
}
/**
* 获取科目名
*
* @return <code>String</code> 科目名
*/
public String getSubName() {
return subName;
}
/**
* 设置科目名
*
* @param subName 科目名
*/
public void setSubName(@NotNull String subName) {
this.subName = subName;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Subject{" +
"subID=" + subID +
", subName='" + subName + '\'' +
'}';
}
}

View File

@@ -0,0 +1,156 @@
package com.cfive.classroom.library.database.bean;
import com.sun.istack.internal.NotNull;
/**
* 教师类,用于教师信息封装
*
* @author FatttSnake
* @version 1.0
*/
public class Teacher {
private final long tchID;
private String tchName;
private Gender gender;
private Faculty faculty;
private String password;
private String salt;
/**
* 构造教师对象
*
* @param tchID 工号,用于识别唯一教师
* @param tchName 教师姓名
* @param gender 性别
* @param faculty 隶属院系
* @param password 密码
* @param salt 加盐
*/
public Teacher(@NotNull long tchID, @NotNull String tchName, @NotNull Gender gender, @NotNull Faculty faculty, @NotNull String password, @NotNull String salt) {
this.tchID = tchID;
this.tchName = tchName;
this.gender = gender;
this.faculty = faculty;
this.password = password;
this.salt = salt;
}
/**
* 获取工号,用于识别唯一教师
*
* @return <code>long</code> 教师编号
*/
public long getTchID() {
return tchID;
}
/**
* 获取教师姓名
*
* @return <code>String</code> 教师姓名
*/
public String getTchName() {
return tchName;
}
/**
* 设置教师姓名
* @param tchName 教师姓名
*/
public void setTchName(@NotNull String tchName) {
this.tchName = tchName;
}
/**
* 获取性别
*
* @return <code>Gender</code> 性别
* @see Gender
*/
public Gender getGender() {
return gender;
}
/**
* 设置性别
*
* @param gender 性别
* @see Gender
*/
public void setGender(@NotNull Gender gender) {
this.gender = gender;
}
/**
* 获取隶属院系
*
* @return <code>Faculty</code> 隶属院系
* @see Faculty
*/
public Faculty getFaculty() {
return faculty;
}
/**
* 设置隶属院系
*
* @param faculty 隶属院系
* @see Faculty
*/
public void setFaculty(@NotNull Faculty faculty) {
this.faculty = faculty;
}
/**
* 获取密码
*
* @return <code>String</code> 密码
*/
public String getPassword() {
return password;
}
/**
* 设置密码
*
* @param password 密码
*/
public void setPassword(@NotNull String password) {
this.password = password;
}
/**
* 获取加盐
*
* @return <code>String</code> 加盐
*/
public String getSalt() {
return salt;
}
/**
* 设置加盐
*
* @param salt 加盐
*/
public void setSalt(@NotNull String salt) {
this.salt = salt;
}
/**
* 仅用于测试使用
*
* @return <code>String</code> 包含所有属性的字符串
*/
@Override
public String toString() {
return "Teacher{" +
"tchID=" + tchID +
", tchName='" + tchName + '\'' +
", gender=" + gender +
", faculty=" + faculty +
", password='" + password + '\'' +
", salt='" + salt + '\'' +
'}';
}
}

View File

@@ -0,0 +1,7 @@
#MySQL Configuration
#Mon Jun 06 15:29:23 CST 2022
port=3306
password=class_five
database=class
ip=10.14.0.7
username=class