Service
接口
//用户service接口 import studio.lyl.questionbankmanagementsystem.entity.User; /** * @Author: Re * @Date: 2020/12/7 22:46 */ public interface IUserService { /** *用户登录 * @param id * @param password * @return boolean * @throws Exception */ boolean loginUserService(Integer id,String password) ; /** * 管理员登录 * @param id * @param password * @return */ boolean loginAdministratorService(Integer id,String password); /** * * @param user * @return boolean * @throws Exception */ boolean registService(User user) ; /** * 创建用户 * @param user * @return boolean */ boolean insertUser(User user) ; /** * 根据id删除用户 * @param id * @return boolean */ boolean deleteUserById(Integer id) ; /** * 根据用户id修改用户信息 * @param id * @return boolean */ boolean updateUser(Integer id,User user) ; /** * 根据id查看用户信息 * @param id * @return User */ User selectUserById(Integer id) ; /** * 查看所有用户信息 * @return * @throws Exception */ String selectUser() ; } //题库service接口 import studio.lyl.questionbankmanagementsystem.entity.Questions; import java.util.List; /** * @Author: Re * @Date: 2020/12/9 15:55 */ public interface IQuestionServise { /** * 增加题目 * @param questions * @return boolean */ boolean insetQuestion(Questions questions) ; /** * 根据序号删除题目 * @param sign * @param type * @return boolean */ boolean deleteQuestion(int sign,String type) ; /** * 根据序号删除题目 * @param sign * @param questions * @param type * @return */ boolean updateQuestion(int sign ,String type ,Questions questions) ; /** * 根据序号查看题目 * @param sign * @param type * @return String */ Questions selectQuestion(int sign, String type) ; /** * 查看一个类型全部的题目 * @param type * @return List */ List<Questions> selectQuestions(String type) ; /** * 根据题目和序号查看改题目的答案 * @param type * @return Questions */ Questions selectAnswer(int sign,String type); } //笔记service接口 package studio.lyl.questionbankmanagementsystem.service; import studio.lyl.questionbankmanagementsystem.entity.Note; import java.util.List; /** * @Author: Re * @Date: 2020/12/9 15:46 */ public interface INoteService { /** * 增加笔记 * @param note * @return boolean */ boolean insetNote(Note note) ; /** * 根据用户id删除笔记 * @param id * @param sign * @return boolean */ boolean deleteNote(Integer id,Integer sign) ; /** * 根据用户id更改笔记 * @param id * @param note * @param sign * @return */ boolean updateNote(Integer id,Integer sign,Note note) ; /** * 根据用户id查看笔记 * @param id * @return */ List<Note> selectNoteById(Integer id) ; /** * 查看所有的笔记 * @return */ List<Note> selectNote() ; }
实现接口
//用户Service接口实现 import studio.lyl.questionbankmanagementsystem.dao.IUserDao; import studio.lyl.questionbankmanagementsystem.dao.impl.UserDaoImpl; import studio.lyl.questionbankmanagementsystem.entity.User; import studio.lyl.questionbankmanagementsystem.service.IUserService; import java.util.List; /** * @Author: Re * @Date: 2020/12/11 20:50 */ public class UserServiceImpl implements IUserService { IUserDao iUserDao=new UserDaoImpl(); @Override public boolean loginUserService(Integer id, String password) { List<User> userList=iUserDao.selectUserById(id); User user=userList.get(0); if(user==null||user.getLevel()==1){ System.out.println("账号错误!"); return false; }else { for (int i = 0; i < userList.size(); i++) { if(user.getPassword().equals(password)){ System.out.println("登录成功。"); return true; } } } System.out.println("密码错误!"); return false; } @Override public boolean loginAdministratorService(Integer id, String password) { List<User> userList=iUserDao.selectUserById(id); if (userList.size()==0){ System.out.println("账号错误"); return false; }else { User user=userList.get(0); if (user == null || user.getLevel() == 0) { System.out.println("账号错误!"); return false; } else { for (int i = 0; i < userList.size(); i++) { if (user.getPassword().equals(password)) { System.out.println("登录成功。"); return true; } } } } System.out.println("密码错误!"); return false; } @Override public boolean registService(User user) { List<User> userList=iUserDao.selectUserById(user.getId()); if(userList.size()==0){ iUserDao.insertUser(user); System.out.println("注册用户成功"); return true; } System.out.println("用户已存在,注册失败"); return false; } @Override public boolean insertUser(User user) { List<User> userList=iUserDao.selectUserById(user.getId()); if(userList.size()==0){ iUserDao.insertUser(user); System.out.println("增加用户成功"); return true; } System.out.println("用户已存在,增加用户失败"); return false; } @Override public boolean deleteUserById(Integer id) { List<User> userList=iUserDao.selectUserById(id); if(userList.size()==0){ System.out.println("用户不存在"); return false; } iUserDao.deleteUserById(id); System.out.println("用户删除成功"); return true; } @Override public boolean updateUser(Integer id, User user) { List<User> userList=iUserDao.selectUserById(id); if(userList.size()==0){ System.out.println("用户不存在,更改用户信息失败"); return false; } iUserDao.updateUser(id, user); System.out.println("更改用户信息成功"); return true; } @Override public User selectUserById(Integer id) { List<User> userList=iUserDao.selectUserById(id); if(userList.size()==0){ return null; }else { User user=userList.get(0); System.out.println(user); return user; } } @Override public String selectUser() { List<User> userList= iUserDao.selectUser(); for (int i = 0; i < userList.size(); i++) { User user=userList.get(i); System.out.println(user); } return userList.toString(); } } //题库Service接口实现 import studio.lyl.questionbankmanagementsystem.dao.IQuestionDao; import studio.lyl.questionbankmanagementsystem.dao.impl.QuestionDaoImpl; import studio.lyl.questionbankmanagementsystem.entity.Questions; import studio.lyl.questionbankmanagementsystem.service.IQuestionServise; import java.util.List; /** * @Author: Re * @Date: 2020/12/9 16:11 */ public class QuestionServiceImpl implements IQuestionServise { IQuestionDao iQuestionDao=new QuestionDaoImpl(); @Override public boolean insetQuestion(Questions questions) { try { iQuestionDao.insetQuestion(questions); } catch (Exception e) { e.printStackTrace(); } return true; } @Override public boolean deleteQuestion(int sign, String type) { List<Questions> questionsList=iQuestionDao.selectQuestion(sign,type); if(questionsList.size()==0){ System.out.println("题目不存在!"); return false; }else { try { iQuestionDao.deleteQuestion(sign, type); } catch (Exception e) { e.printStackTrace(); } System.out.println("题目已删除"); return true; } } @Override public boolean updateQuestion(int sign, String type, Questions questions) { List<Questions> questionsList=iQuestionDao.selectQuestion(sign, type); if(questionsList.size()==0){ System.out.println("题目不存在!"); return false; }else { try { iQuestionDao.deleteQuestion(sign, type); iQuestionDao.insetQuestion(questions); } catch (Exception e) { e.printStackTrace(); } System.out.println("题目更改成功。"); return true; } } @Override public Questions selectQuestion(int sign, String type) { List<Questions> questionsList=iQuestionDao.selectQuestion(sign, type); if(questionsList.size()==0){ return null; }else { Questions questions=questionsList.get(0); return questions; } } @Override public List<Questions> selectQuestions(String type) { List<Questions> questionsList=iQuestionDao.selectQuestions(type); if(questionsList.size()==0){ System.out.println("该题库暂无题目"); return null; }else { for (int i = 0; i < questionsList.size(); i++) { System.out.println(questionsList.get(i)); } return questionsList; } } @Override public Questions selectAnswer( int sign,String type) { List<Questions> questionsList=iQuestionDao.selectQuestion(sign,type); if(questionsList.size()==0){ System.out.println("该题库暂无题目"); return null; }else { Questions questions=questionsList.get(0); System.out.println("本题答案为: "+questions.getAnswer()); return null; } } } //笔记Service接口实现 import studio.lyl.questionbankmanagementsystem.dao.INoteDao; import studio.lyl.questionbankmanagementsystem.dao.impl.NoteDaoImpl; import studio.lyl.questionbankmanagementsystem.entity.Note; import studio.lyl.questionbankmanagementsystem.service.INoteService; import java.util.ArrayList; import java.util.List; /** * @Author: Re * @Date: 2020/12/11 20:17 */ public class NoteServiceImpl implements INoteService { INoteDao iNoteDao=new NoteDaoImpl(); @Override public boolean insetNote(Note note) { iNoteDao.insetNote(note); System.out.println("添加笔记成功"); return true; } @Override public boolean deleteNote(Integer id,Integer sign) { List<Note> noteList=iNoteDao.selectNoteById(id); if(noteList.size()==0){ System.out.println("笔记不存在!"); return false; }else { iNoteDao.deleteNote(id,sign); System.out.println("笔记已删除。"); return true; } } @Override public boolean updateNote(Integer id,Integer sign, Note note) { List<Note> noteList = iNoteDao.selectNoteById(id); if (noteList.size() == 0) { System.out.println("笔记不存在!"); return false; } else { List<Note> noteListBySign = new ArrayList<>(); for (int i = 0; i < noteList.size(); i++) { Note noteBySign = noteList.get(i); if (noteBySign.getSign() == sign) { noteListBySign.add(noteBySign); } } if (noteListBySign.size() == 0) { System.out.println("笔记不存在"); } else { iNoteDao.updateNote(id, sign, note); System.out.println("笔记已更改。"); } } return true; } @Override public List<Note> selectNoteById(Integer id) { List<Note> noteList=iNoteDao.selectNoteById(id); if(noteList.size()==0){ return null; }else { return noteList; } } @Override public List<Note> selectNote() { List<Note> noteList=iNoteDao.selectNote(); if(noteList.size()==0){ System.out.println("现无笔记存在。"); return null; }else { return noteList; } } }
Controller
//用户Controller层 import studio.lyl.questionbankmanagementsystem.entity.User; import studio.lyl.questionbankmanagementsystem.service.IUserService; import studio.lyl.questionbankmanagementsystem.service.impl.UserServiceImpl; /** * @Author: Re * @Date: 2020/12/11 22:05 */ public class UserController { IUserService iUserService=new UserServiceImpl(); /** *登录用户 * @param id * @param password * @return boolean * @throws Exception */ public boolean loginUser(Integer id,String password) { return iUserService.loginUserService(id, password); } public boolean loginAdministrator(Integer id,String password){ return iUserService.loginAdministratorService(id,password); } /** *注册用户 * @param user * @return boolean * @throws Exception */ public boolean registUser(User user) { return iUserService.registService(user); } /** * 创建用户 * @param user * @return boolean */ public boolean insertUser(User user) { return iUserService.insertUser(user); } /** * 根据id删除用户 * @param id * @return boolean */ public boolean deleteUserById(Integer id) { return iUserService.deleteUserById(id); } /** * 根据用户id修改用户信息 * @param id * @return boolean */ public boolean updateUser(Integer id,User user) { return iUserService.updateUser(id, user); } /** * 根据id查看用户信息 * @param id * @return String */ public User selectUserById(Integer id) { return iUserService.selectUserById(id); } /** * 查看所有用户信息 * @return * @throws Exception */ public String selectUser() { return iUserService.selectUser(); } } //题库controller层 import studio.lyl.questionbankmanagementsystem.entity.Questions; import studio.lyl.questionbankmanagementsystem.service.IQuestionServise; import studio.lyl.questionbankmanagementsystem.service.impl.QuestionServiceImpl; import java.util.List; /** * @Author: Re * @Date: 2020/12/11 22:02 */ public class QuestionsController { IQuestionServise iQuestionServise=new QuestionServiceImpl(); /** * 增加题目 * @param questions * @return boolean */ public boolean insetQuestion(Questions questions) { return iQuestionServise.insetQuestion(questions); } /** * 根据序号删除题目 * @param sign * @param type * @return boolean */ public boolean deleteQuestion(int sign,String type) { return iQuestionServise.deleteQuestion(sign, type); } /** * 根据序号更改题目 * @param sign * @param questions * @param type * @return */ public boolean updateQuestion(int sign ,String type ,Questions questions) { return iQuestionServise.updateQuestion(sign, type, questions); } /** * 根据序号查看题目 * @param sign * @param type * @return question */ public Questions selectQuestion(int sign, String type) { return iQuestionServise.selectQuestion(sign, type); } /** * 查看一个类型中所有的题目 * @param type * @return * @throws Exception */ public List<Questions> selectQuestions(String type) { return iQuestionServise.selectQuestions(type); } /** * 根据题目类型和序号查看题目答案 * @param type * @return Question */ public Questions selectAnswer(int sign,String type){ return iQuestionServise.selectAnswer(sign , type); } } //笔记controller层 package studio.lyl.questionbankmanagementsystem.controller; import studio.lyl.questionbankmanagementsystem.entity.Note; import studio.lyl.questionbankmanagementsystem.service.INoteService; import studio.lyl.questionbankmanagementsystem.service.impl.NoteServiceImpl; import java.util.List; /** * @Author: Re * @Date: 2020/12/11 21:58 */ public class NoteController { INoteService iNoteService=new NoteServiceImpl(); /** * 增加笔记 * @param note * @return boolean */ public boolean insetNote(Note note) { return iNoteService.insetNote(note); } /** * 根据用户id删除笔记 * @param id * @return boolean */ public boolean deleteNote(Integer id,Integer sign) { return iNoteService.deleteNote(id,sign); } /** * 根据用户id更改笔记 * @param id * @param sign * @param note * @return */ public boolean updateNote(Integer id,Integer sign,Note note) { return iNoteService.updateNote(id,sign ,note); } /** * 根据用户id查看笔记 * @param id * @return */ public List<Note> selectNoteById(Integer id) { return iNoteService.selectNoteById(id); } /** * 查看所有的笔记 * @return */ public List<Note> selectNote() { return iNoteService.selectNote(); } }