A role-based employee attendance management system was implemented using Java Servlets, MyBatis, and MySQL. The system supports login, personal information management, password updates, and attendance tracking with differentiated views for employees, department managers, and administrators.
The data model consists of two primary entities:
Staff: stores employee details including job ID, name, gender, birth date, department, role, and password.Record: logs daily attendance entries with timestamps, job ID, personal details, department, and attendance type (e.g., "上班" or "下班").
// Staff entity
public class Staff {
private String jobid;
private String name;
private String sex;
private String birthday;
private String department;
private String role;
private String password;
// getters and setters
}
// Attendance record entity
public class AttendanceRecord {
private int id;
private String attendancetime;
private String jobid;
private String name;
private String sex;
private String birthday;
private String department;
private String attendancetype;
// getters and setters
}
The UserMapper interface defines database operations:
public interface UserMapper {
Staff authenticate(@Param("jobid") String jobid, @Param("password") String password);
Staff findStaffByJobId(@Param("jobid") String jobid);
int updateProfile(@Param("jobid") String jobid, @Param("name") String name,
@Param("sex") String sex, @Param("birthday") String birthday);
int changePassword(@Param("jobid") String jobid, @Param("oldPass") String oldPass,
@Param("newPass") String newPass);
int logAttendance(@Param("id") Integer id, @Param("time") String time,
@Param("jobid") String jobid, @Param("name") String name,
@Param("sex") String sex, @Param("birthday") String birthday,
@Param("dept") String dept, @Param("type") String type);
List<AttendanceRecord> getAttendanceHistory(@Param("jobid") String jobid);
}
Login functionality routes users based on their role:
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String jobid = req.getParameter("jobid");
String password = req.getParameter("password");
try (InputStream configStream = Resources.getResourceAsStream("mybatis-config.xml")) {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(configStream);
try (SqlSession session = factory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
Staff user = mapper.authenticate(jobid, password);
if (user != null) {
switch (user.getRole()) {
case "员工" -> resp.sendRedirect("/job-demo/staffscreen.html");
case "部门经理" -> resp.sendRedirect("/job-demo/managerscreen.html");
case "管理员" -> resp.sendRedirect("/job-demo/adminscreen.html");
}
} else {
resp.getWriter().write("Login failed!");
}
}
}
}
}
Profile editing and password change are handled by dedicated servlets. The password update includse validation to ensure the new password differs from the old one and matches the confirmation input.
Attendance logging uses client-side JavaScript to capture Beijing time and distinguish between clock-in and clock-out actions:
<script>
function submitAttendance(type) {
document.getElementById('attendancetype').value = type;
document.getElementById('attendance-form').submit();
}
</script>
MyBatis XML mappings define SQL operations:
<mapper namespace="job.mapper.UserMapper">
<insert id="logAttendance">
INSERT INTO record(id, attendancetime, jobid, name, sex, birthday, department, attendancetype)
VALUES (#{id}, #{time}, #{jobid}, #{name}, #{sex}, #{birthday}, #{dept}, #{type})
</insert>
<update id="updateProfile">
UPDATE staff SET name=#{name}, sex=#{sex}, birthday=#{birthday} WHERE jobid=#{jobid}
</update>
<update id="changePassword">
UPDATE staff SET password=#{newPass} WHERE jobid=#{jobid} AND password=#{oldPass}
</update>
<select id="authenticate" resultType="job.pojo.Staff">
SELECT * FROM staff WHERE jobid=#{jobid} AND password=#{password}
</select>
<select id="getAttendanceHistory" resultType="job.pojo.AttendanceRecord">
SELECT * FROM record WHERE jobid=#{jobid}
</select>
</mapper>
Database connection is configured in mybatis-config.xml with JDBC settings for a local MySQL instance. Frontend pages provide role-specific navigation and form inputs for all supported operations.