diff --git a/.idea/misc.xml b/.idea/misc.xml index 1e23d89f341196be26c576d27b10d063c8809cfe..aaba9f1adbc685747778f82c0151897d671d2994 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,10 @@ - + + + \ No newline at end of file diff --git a/StudentManagement.iml b/StudentManagement.iml index c90834f2d607afe55e6104d8aa2cdfffb713f688..c3dc060f7602361ec6554eeadb599d415a4d0f6f 100644 --- a/StudentManagement.iml +++ b/StudentManagement.iml @@ -7,5 +7,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/edu/tyut/selaba2/student/StudentManageApplication.java b/src/edu/tyut/selaba2/student/StudentManageApplication.java index 308e116e3a2fb08d055e9598358ab5c0773b739a..062c135502639b870969369edd941bf7aaa5d0e3 100644 --- a/src/edu/tyut/selaba2/student/StudentManageApplication.java +++ b/src/edu/tyut/selaba2/student/StudentManageApplication.java @@ -7,5 +7,6 @@ package edu.tyut.selaba2.student; public class StudentManageApplication { public static void main(String[] args) { + } } diff --git a/src/edu/tyut/selaba2/student/service/impl/StudentServiceImpl.java b/src/edu/tyut/selaba2/student/service/impl/StudentServiceImpl.java index 3d1423fceb862f63624f40595f381f0858eca36d..40abb892b98abb6b09ab4e7226180ba5494b9edf 100644 --- a/src/edu/tyut/selaba2/student/service/impl/StudentServiceImpl.java +++ b/src/edu/tyut/selaba2/student/service/impl/StudentServiceImpl.java @@ -4,12 +4,24 @@ import edu.tyut.selaba2.student.entity.Student; import edu.tyut.selaba2.student.repository.StudentRepository; import edu.tyut.selaba2.student.service.StudentService; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.List; +import java.util.Locale; +/** + * 实现StudentService接口 + */ public class StudentServiceImpl implements StudentService { private final StudentRepository studentRepository; - StudentServiceImpl(StudentRepository studentRepository){ - this.studentRepository=studentRepository; + + /** + * 注入依赖 + * @param studentRepository 实现了studentRepository接口的实例 + */ + public StudentServiceImpl(StudentRepository studentRepository) { + this.studentRepository = studentRepository; } @Override @@ -19,30 +31,85 @@ public class StudentServiceImpl implements StudentService { @Override public List findStudentByName(String name) { - - Student student = new Student(); - student.setName(name); - return studentRepository.findStudentByExample(student); + Student exampleStudent = new Student(); + exampleStudent.setName(name); + return studentRepository.findStudentByExample(exampleStudent); } @Override - public List findStudentByClassNumberAndDepartment(Integer classNumber, String department) { - return null; + public List findStudentByClassNumberAndDepartment(Integer classNumber, String department) { //@liaoyu + + //设置学生信息 + Student student = new Student(); + student.setClassNumber(classNumber); + student.setDepartment(department); + + //通过studentRepository接口的findStudentByExample()查找. + List findSt = studentRepository.findStudentByExample(student); + + return findSt; } @Override - public Boolean addStudent(Student student) { - return null; + public Boolean addStudent(Student student) { // @liaoyu + + //如果学生的Uid存在,则返回false + if (student.getUid() != null) { + return false; + } + //插入学生信息.并返回true. + studentRepository.insert(student); + + return true; } @Override public Integer addStudent(List students) { - return null; + int count = 0; + for (Student st : students) { + if (studentRepository.insert(st)) { + count++; + } + } + return count; } @Override public Boolean updateStudent(Long uid, String field, Object o) { - return null; + if (studentRepository.getStudentByUid(uid) == null) { + return false;//避免空指针 + } + Student student = studentRepository.getStudentByUid(uid); + try { + String methodName = "set" + field.substring(0, 1).toUpperCase(Locale.ROOT) + field.substring(1); + Class argType; + //校验属性 + switch (field) { + case "name": + case "department": + argType = String.class; + break; + case "classNumber": + case "sex": + case "age": + argType = Integer.class; + break; + default: + return false; + } + //校验参数 + if (!o.getClass().equals(argType)) { + return false; + } + //获取对应方法 + Method met = student.getClass().getMethod(methodName, argType); + //调用方法 + met.invoke(student, o); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + return false; + } + //更新 + return studentRepository.update(student); } @Override @@ -57,8 +124,6 @@ public class StudentServiceImpl implements StudentService { @Override public Boolean deleteStudentById(Long uid) { - if(studentRepository.delete(uid)) - return true; - else return false; + return studentRepository.delete(uid); } }