Java 實戰項目之教材管理系統的實現流程

一、項目簡述

功能包括: 管理員可以增刪改查教材、教材商、入庫教材、用戶(用 戶包括學生和教師)可以對教材商、教材進行。Excel的導入 導出操作。教師以領取入庫的教材,可以退還教材。學生隻能在對應的教師那裡領取教材,並且可以退還教材、 查詢自己已經領取的教材。並且對已領教材付款等等。

二、項目運行

環境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)

項目技術: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。

查看班級選報信息:

 
@Service
public class TopicsServiceImpl implements TopicsService {
 
    @Autowired
    private MyClassMapper classMapper;
 
    @Autowired
    private StudentMapper studentMapper;
 
    @Autowired
    private SubjectselectedMapper subjectselectedMapper;
 
    @Autowired
    private SubjectMapper subjectMapper;
 
    @Autowired
    private HttpServletRequest request;
 
    /**
     * 查看班級選報信息
     */
    @Override
    public List<classinfo> classinfo(Long id) {
        MyClass className = classMapper.selectById(id);//查到班級名字
        List<Student> students = studentMapper.selectByListAll(id);//根據student表裡班級id查到用戶所在班級的所有人
        List<Subjectselected> projectselectedlist = new ArrayList<>();
        List<Subject> projectlist = new ArrayList<>();
        Subjectselected projectselected = null;
        Subject project = null;
        for (int i = 0; i < students.size(); i++) {
            List<Subjectselected> tprojectselecteds = subjectselectedMapper.selectBystudentid(students.get(i).getId());
            if (tprojectselecteds != null && tprojectselecteds.size() > 0)
                projectselected = tprojectselecteds.get(0);
            if (projectselected != null) {
                projectselectedlist.add(projectselected);
            } else {
                projectselected = new Subjectselected();
                projectselected.setId(0L);
                projectselected.setIdProject(0L);
                projectselected.setIdStudent(0L);
                projectselected.setStuselectFlag(0L);
                projectselectedlist.add(projectselected);
            }
        }
        for (int i = 0; i < projectselectedlist.size(); i++) {
            if (projectselectedlist.get(i).getId() != 0) {
                projectlist.add(subjectMapper.selectById(projectselectedlist.get(i).getIdProject()));
            } else {
                project = new Subject();
                project.setProjectname("無");
                project.setTeachernames("無");
                projectlist.add(project);
            }
        }
        List<classinfo> infolist = new ArrayList<>();
        classinfo info = null;
        for (int i = 0; i < projectlist.size(); i++) {
            info = new classinfo();
            info.setId(students.get(i).getId());
            info.setClassName(className.getClassName());
            info.setName(students.get(i).getName());
            info.setStuNum(students.get(i).getStunum());
            info.setProjectName(projectlist.get(i).getProjectname());
            info.setTeacherNames(projectlist.get(i).getTeachernames());
            info.setStuSelectFlag(projectselectedlist.get(i).getStuselectFlag());
            infolist.add(info);
        }
        return infolist;
    }
 
    /**
     * 查看課題信息
     * 通過classid
     */
    @Override
    public List<topicsinfo> topics(Long id) {
        String specialty = classMapper.selectByspecialty(id);
        List<Subject> projectlist = subjectMapper.selectByAll("%" + specialty + "%");
        List<topicsinfo> topicsinfolist = new ArrayList<>();
        topicsinfo info = null;
        for (int i = 0; i < projectlist.size(); i++) {
            info = new topicsinfo();
            info.setProject_id(projectlist.get(i).getId());
            info.setProjectType(subjectMapper.selectByprojectType(projectlist.get(i).getIdProjecttype().intValue()));
            info.setProjectSource(subjectMapper.selectByprojectSource(projectlist.get(i).getIdProjectsource().intValue()));
            info.setProjectName(projectlist.get(i).getProjectname());
            info.setSelectFlag(projectlist.get(i).getSelectFlag());
            info.setMarchSpecialty(projectlist.get(i).getMarchspecialty());
            info.setTeacherNames(projectlist.get(i).getTeachernames());
            topicsinfolist.add(info);
        }
        return topicsinfolist;
    }
 
    /**
     * 課題具體信息
     */
    @Override
    public List<topicsto> topicsinfo(Long project_id) {
        List<Subjectselected> listed = subjectselectedMapper.selectByprojectid(project_id);//中間表
        request.getSession().setAttribute("listsize",listed.size());
 
        List<topicsto> tolist = new ArrayList<>();
        topicsto to = null;
        Student student = null;
        for (int i = 0; i < listed.size(); i++) {
            to = new topicsto();
            student = studentMapper.selectByid(listed.get(i).getIdStudent());
            if(student == null)
                continue;
            to.setStuNum(student.getStunum());
            to.setName(student.getName());
            to.setStuSelectFlag(listed.get(i).getStuselectFlag());
            to.setClassName(classMapper.selectById(student.getIdClass()).getClassName());
            tolist.add(to);
        }
        return tolist;
    }
 
    /**
     * 獲取該用戶是否選報瞭課題
     */
 
    public Long state(Student student) {
        Subjectselected project = null;
        List<Subjectselected> tprojectselecteds = subjectselectedMapper.selectBystudentid(student.getId());
        if (tprojectselecteds != null && tprojectselecteds.size() > 0)
            project = tprojectselecteds.get(0);
        if (project == null) {
            return 0L;
        } else
            return project.getStuselectFlag();
    }
 
 
    /**
     * 添加選題
     */
    @Override
    public void insertproject(String project, Long studentid) {
        Long projectid = subjectMapper.selectByprojectid(project);
        int flagid = subjectselectedMapper.insertenroll(projectid, studentid);
    }
 
    /**
     * 刪除選題
     */
    @Override
    public int deleteprojectselectedid(Long id) {
        int flag = subjectselectedMapper.deleteBystudentId(id);
        return flag;
    }
 
    /**
     * 查詢課題名
     *
     */
    public String selectprojectname(Long id){
        String project_Name = subjectMapper.selectById(id).getProjectname();
        return project_Name;
    }
 
 
    /**
     * 獲取該題是否可選
     */
    @Override
    public Long flag(Long project_id) {
        return subjectMapper.selectByselectFlag(project_id);
    }
}

班級信息查詢業務:

@Service
public class ClassServiceImpl implements ClassService {
 
    @Autowired
    MyClassMapper myClassMapper;
    @Autowired
    SpecialtyService specialtyService;
    @Autowired
    StudentService studentService;
 
    @Override
    public List getClasses(Integer offset, String getName, Long specialtyid, Long id_institute) {
 
        // 設置分頁查詢條件
        MyClassExample myClassExample = new MyClassExample();
        //  添加查詢條件
        myClassExample = addCondition(myClassExample,offset,id_institute,getName,specialtyid);
 
        if(myClassExample==null)
            return null;
 
        //如果 offset 為 -1 則隻查詢 id 和 name 列
        if(offset==-1)
        {
            return myClassMapper.selectByExampleSelective(myClassExample,
                    MyClass.Column.id,
                    MyClass.Column.className);
        }else{
            return myClassMapper.selectByExampleSelective(myClassExample);
        }
 
    }
 
    @Override
    public long getClassesCount(Integer offset, String getName, Long specialtyid, Long id_institute) {
        // 設置分頁查詢條件
        MyClassExample myClassExample = new MyClassExample();
        //        添加查詢條件
        myClassExample = addCondition(myClassExample,offset,id_institute,getName,specialtyid);
 
        if(myClassExample==null)
            return 0;
 
        return myClassMapper.countByExample(myClassExample);
 
    }
 
    @Override
    public long delClass(MyClass myClass, Long id_institute) throws MyException {
        checkSpecialtyId(myClass,id_institute);
        return myClassMapper.deleteByPrimaryKey(myClass.getId());
    }
 
    @Override
    public long putClass(MyClass myClass, Long id_institute) throws MyException {
        checkSpecialtyId(myClass,id_institute);
        return myClassMapper.updateByPrimaryKey(myClass);
    }
 
    @Override
    public long postClass(MyClass myClass, Long id_institute) throws MyException {
        checkSpecialtyId(myClass,id_institute);
        long count = getClassesCount(1,myClass.getClassName(),null, id_institute);
        if(count!=0)
            throw new MyException("該班級已存在");
        return myClassMapper.insert(myClass);
    }
 
    @Override
    public long delClassBySpecialty(Long specialtyid) {
        return myClassMapper.deleteByExample(new MyClassExample().createCriteria().andIdSpecialtyEqualTo(specialtyid).example());
    }
 
    public MyClassExample addCondition(MyClassExample myClassExample,Integer offset , Long id_institute , String keyWord , Long id_specialty){
 
        //代表為超管,則可以查詢所有;
        if(id_institute==-1)
        {
            return myClassExample.newAndCreateCriteria()
                    .when(StringUtil.stringIsNotNull(keyWord), criteria -> criteria.andClassNameEqualTo(keyWord)) //根據關鍵字查詢
                    .when(id_specialty!=null,criteria -> criteria.andIdSpecialtyEqualTo(id_specialty)) //根據指定教研室查詢
                    .example()
                    .when(offset!=-1 && offset != 0,criteria -> criteria.page(offset-1, 10));
        }
 
        //查詢出來當前學院的所有專業
        List<Specialty> specialties = specialtyService.getSpecialtys(-1,null,null,id_institute);
 
        //提取 id列 列表
        List<Long> specialtiesIDs = specialties.stream().map(Specialty::getId).collect(Collectors.toList());
 
        if(specialtiesIDs.size()==0||specialtiesIDs==null)
            return null;
 
        return myClassExample.newAndCreateCriteria()
                .when(StringUtil.stringIsNotNull(keyWord),criteria -> criteria.andClassNameEqualTo(keyWord)) //根據關鍵字查詢
                .when(id_specialty!=null,criteria -> criteria.andIdSpecialtyEqualTo(id_specialty)) //根據指定教研室查詢
                // 查詢的專業隻能在當前的學院的教研室下
                .andIdSpecialtyIn(specialtiesIDs)
                .example()
                .when(offset!=-1 && offset != 0,criteria -> criteria.page(offset-1, 10));// 查詢第offest-1頁數據(每頁10條)
 
    }
 
    public void checkSpecialtyId(MyClass myClass ,Long id_institute) throws MyException {
        //如果為超管則不需要檢查有沒有權力
        if(id_institute==-1)
        {
            return ;
        }
 
        long id_specialty = myClass.getIdSpecialty();
        List<Specialty> specialtyes = specialtyService.getSpecialtys(1,null,null,id_institute);
        //如果被刪除的專業是不屬於本系教研室則提示沒有權限
        for(Specialty specialty :specialtyes)
        {
            if(specialty.getId()==id_specialty)
                return;
        }
        throw new MyException("沒有權限操作");
    }
 
}

管理員業務操作:

@Service
public class AdminServiceImpl implements AdminService {
 
    @Autowired
    AdminMapper adminMapper;
 
    @Autowired
    SadminMapper sadminMapper;
 
    @Autowired
    GeneralPurposeMapper mapper;
 
    @Autowired
    PublicService publicService;
 
    //Session 保存時間(秒)
    private final Integer SAVE_TIME = 60*60*24;
 
    //超管 和 子管用一個登錄
    @Autowired
    StudentMapper studentMapper;
 
    @Autowired
    SubjectselectedMapper subjectselectedMapper;
 
    @Autowired
    SubjectMapper subjectMapper;
    @Autowired
    ProjectMapper projectMapper;
 
    @Autowired
    ClassMapper classMapper;
 
 
    @Autowired
    SpecialtyMapper specialtyMapper;
 
    @Autowired
    InstituteMapper instituteMapper;
 
    @Autowired
    HttpServletRequest request;
 
    @Autowired
    HttpServletResponse response;
 
    @Override
    public Msg login(String name, String pwd , HttpSession httpSession) {
 
        Sadmin sadmin = selectSadminByName(name);
        if(sadmin!=null){
            //驗證密碼是否與數據庫中的相同
            if(sadmin.getPwd().equals(pwd)){
                User user = new User();
                //-1表示為超管
                user.setId(-1L);
                user.setRole("sadmin");
                user.setUserName(sadmin.getName());
                //生成Token 存到 Cookie
                Cookie cookie = new Cookie("token",TokenUtil.createToken(
                        user
                ));
                //該Cookie無法被js讀取
                cookie.setHttpOnly(true);
                cookie.setPath("/");
                response.addCookie(cookie);
                return Msg.success();
            }
            return Msg.error("密碼錯誤");
        }else {
            Admin admin = selectByName(name);
            if(admin == null){
                return Msg.error("賬號不存在");
            }else {
                if(admin != null && admin.getPwd().equals(pwd)){
                    User user = new User();
                    user.setId(admin.getId());
                    user.setRole("admin");
                    user.setUserName(admin.getName());
                    Cookie cookie = new Cookie("token",TokenUtil.createToken(
                            user
                    ));
                    cookie.setPath("/");
                    cookie.setHttpOnly(true);
                    response.addCookie(cookie);
                    return Msg.success();//賬號密碼正確
                }
                return Msg.error("密碼錯誤");
            }
 
        }
 
 
    }
 
 
 
    @Override
    public Admin selectByName(String name) {
        //創建一個 Admin 實例 ,添加條件 為 Name == name
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andNameEqualTo(name);
        //根據實例 來查詢
        List<Admin> admin = adminMapper.selectByExample(adminExample);
        if( admin==null || admin.size()==0 ) {
            return null;
        }
        return admin.get(0);
    }
 
    @Override
    public Sadmin selectSadminByName(String name) {
        SadminExample sadminExample = new SadminExample();
        sadminExample.createCriteria().andNameEqualTo(name);
        List<Sadmin> sadmin = sadminMapper.selectByExample(sadminExample);
        if( sadmin==null|| sadmin.size()==0 )
            return null;
        return sadmin.get(0);
    }
 
    @Override
    public int updatePwdByUserName(String name, String pwd) {
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andNameEqualTo(name);
        Admin admin = new Admin();
        admin.setPwd(pwd);
        adminMapper.updateByExampleSelective(admin,adminExample);
 
        return 0;
    }
 
    @Override
    public int updateSadminPwdByUserName(String name, String pwd) {
        SadminExample sadminExample = new SadminExample();
        sadminExample.createCriteria().andNameEqualTo(name);
        Sadmin sadmin = new Sadmin();
        sadmin.setPwd(pwd);
        return sadminMapper.updateByExampleSelective(sadmin, sadminExample);
    }
 
    // 班級的增加
    @Override
    public int add_class(Long specialty_id, String class_name) {
        List<Class> classes = select_class(null, null, null, null, class_name);
        if (classes == null || classes.size() == 0) {
            Class class1 = new Class();
            class1.setClassName(class_name);
            class1.setIdSpecialty(specialty_id);
            return classMapper.insert(class1);
        }
        return 0;
    }
 
    @Override
    public int addAdmin(Admin admin) {
        //添加的時候檢測又沒用戶名重復的
        publicService.CheckIfTheUsernameIsDuplicated(admin.getName());
        return adminMapper.insert(admin);
    }
 
    @Override
    public int delAdmin(Admin admin) {
        return adminMapper.deleteByPrimaryKey(admin.getId());
    }
 
    @Override
    public int updateAdmin(Admin admin) {
        //不更新username
        admin.setName(null);
        return adminMapper.updateByPrimaryKeySelective(admin);
    }
 
    @Override
    public List selectAdmins(Long offset) {
 
 
 
        if(offset!=null && offset==-1){
            return adminMapper.selectByExampleSelective(
                    new AdminExample(),
                    Admin.Column.id
            );
        }
 
        return adminMapper.selectByExample(
                new AdminExample()
        );
    }
 
 
    // 學生的增刪改查
    @Override
    public int add_student(String userName, String stuNum, Long id_class, String name, String gender, String pwd){
        Student ss = studentMapper.select_studentbyName(userName, stuNum);
        if (ss == null) {
            StudentWithBLOBs student = new StudentWithBLOBs();
            student.setUsername(userName);
            student.setStunum(stuNum);
            student.setIdClass(id_class);
            student.setName(name);
            student.setGender(gender);
            student.setPwd(pwd);
            return studentMapper.insert(student);
        }
        return 0;
    }
    public int delete_class(Long class_id) {
        return classMapper.deleteByPrimaryKey(class_id);
    }
 
    @Override
    public int update_class(Long class_id, Long specialty_id, String class_name) {
        Class class1 = new Class();
        class1.setId(class_id);
        class1.setIdSpecialty(specialty_id);
        class1.setClassName(class_name);
        return classMapper.updateByPrimaryKeySelective(class1);
    }
 
    @Override
    public List<Project> select_project(Long institute_id, Long section_id, String section_name) {
        return projectMapper.select_project(institute_id, null, null);
    }
 
    @Override
    public List<Static_student> select_studentXT_all(Long section_id, String section_name, Long specialty_id, String specialty_name, Long class_id, String class_name) {
        List<Static_student> static_students = studentMapper.select_studentXT_all(section_id, section_name, specialty_id, specialty_name, class_id, class_name);
        if (static_students != null && static_students.size() > 0) {
            for (int i = 0; i < static_students.size(); i++) {
                List<Subjectselected> projectselecteds = select_Projectselected(static_students.get(i).getId(), null);
                if (projectselecteds != null && projectselecteds.size() > 0) {
                    static_students.get(i).setStuselectFlag("" + projectselecteds.get(0).getStuselectFlag());
                    static_students.get(i).setProjectname(projectselecteds.get(0).getProject_name());
                    static_students.get(i).setTeachernames(projectselecteds.get(0).getProject_teachers());
                } else {
                    static_students.get(i).setProjectname("未選題");
                    static_students.get(i).setTeachernames("");
                }
            }
        }
        return static_students;
    }
 
 
    @Override
    public List<Class> select_class(Long institute_id, Long section_id, Long specialty_id, Long class_id, String class_name) {
        return classMapper.select_class(institute_id, section_id, specialty_id, class_id, class_name);
    }
 
    // 專業方向的增刪改查
    @Override
    public int add_specialty(Long section_id, String specialty_name) {
        List<Specialty> specialties = select_specialty(null, null, null, specialty_name);
        if (specialties == null || specialties.size() == 0) {
            Specialty specialty = new Specialty();
            specialty.setIdSection(section_id);
            specialty.setSpecialtyName(specialty_name);
            return specialtyMapper.insert(specialty);
        }
        return 0;
    }
 
    @Override
    public int delete_student(Long student_id) {
        return studentMapper.deleteByPrimaryKey(student_id);
    }
 
    @Override
    public int update_student(Long student_id, String userName, String stuNum, Long id_class, String name, String gender, String pwd) {
        Student student = new Student();
        student.setId(student_id);
        student.setUsername(userName);
        student.setStunum(stuNum);
        student.setIdClass(id_class);
        student.setName(name);
        student.setGender(gender);
        student.setPwd(pwd);
        return studentMapper.updateByPrimaryKeySelective((StudentWithBLOBs) student);
    }
 
    @Override
    public List<Static_student> select_student(Long institute_id, Long specialty_id, Long class_id, Long student_id, String name) {
        List<Static_student> static_students = studentMapper.select_Student(institute_id, specialty_id, class_id, student_id, name);
        if (static_students != null) {
            for (int i = 0; i < static_students.size(); i++) {
                Static_student static_student = static_students.get(i);
 
                List<Subjectselected> projectselected = select_Projectselected(static_student.getId(), null);
 
                if (projectselected != null && projectselected.size() != 0) {
                    String s = "";
                    if (projectselected.get(0).getStuselectFlag() == 1) s = "選題待審核";
                    else if (projectselected.get(0).getStuselectFlag() == 2) s = "選題未通過";
                    else s = "選題通過";
                    static_student.setStuselectFlag(s);
                    static_student.setProjectname(projectselected.get(0).getProject_name());
                    static_student.setTeachernames(projectselected.get(0).getProject_teachers());
 
 
                }
            }
            return static_students;
        }
        return null;
    }
 
 
    // 通過學生id 或者 課題id 查 課題的選擇 情況
    public List<Subjectselected> select_Projectselected(Long student_id, Long project_id) {
        return subjectselectedMapper.select_Projectselected(student_id, project_id);
    }
 
    public List<Subject> select_ProjectXQ(Long institute_id, Long section_id, String name) {
        List<Subject> projects = subjectMapper.select_ProjectXQ(institute_id, section_id, name);
        if (projects != null) {
            for (int i = 0; i < projects.size(); i++) {
                if (projects.get(i).getSelectFlag() == 0) projects.get(i).setProjectGB("可選");
                else if (projects.get(i).getSelectFlag() == 1) projects.get(i).setProjectGB("不可選");
            }
        }
        return projects;
    }
    public int delete_specialty(Long specialty_id) {
        return specialtyMapper.deleteByPrimaryKey(specialty_id);
    }
 
    @Override
    public int update_specialty(Long specialty_id, Long section_id, String specialty_name) {
        Specialty specialty = new Specialty();
        specialty.setId(specialty_id);
        specialty.setIdSection(section_id);
        specialty.setSpecialtyName(specialty_name);
 
        return specialtyMapper.updateByPrimaryKeySelective(specialty);
    }
 
    @Override
    public List<Specialty> select_specialty(Long institute_id, Long section_id, Long specialty_id, String specialty_name) {
        return specialtyMapper.select_specialty(institute_id, section_id, specialty_id, specialty_name); // 根據學院id 或者 教研室id 查所有專業方向
    }
 
    @Override
    public List<Institute> select_institute(Long institute_id, String institute_name) {
        InstituteExample instituteExample = new InstituteExample();
        InstituteExample.Criteria criteria = instituteExample.createCriteria();
        if (institute_id != null && institute_id != 0) criteria.andIdEqualTo(institute_id);
        if (institute_name != null && institute_name.length() > 0) criteria.andInstituteNameEqualTo(institute_name);
        return instituteMapper.selectByExample(instituteExample);
    }
 
 
 
}

到此這篇關於Java 實戰項目之教材管理系統的實現流程的文章就介紹到這瞭,更多相關Java 教材管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: