packageorg.java.test;importjava.util.ArrayList;importjava.util.List;importorg.junit.Before;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.springframework.transaction.annotation.Transactional;importcom.java.web.Clazzes;importcom.java.web.ClazzesService;importcom.java.web.Student;importcom.java.web.StudentDao;importcom.java.web.StudentService;public classTestInertStudentClass {
ApplicationContext ac=null;
@Beforepublic voidbefort(){
ac=new ClassPathXmlApplicationContext(new String[]{"application-config.xml","dispatcher-servlet.xml","dispatcher-shiro.xml"});
}/*** 添加学生并且绑定班级
*@throwsException*/@Testpublic void InsertStudent() throwsException {
StudentDao studentdao=(StudentDao) ac.getBean("studentDaoImpl");
List studentList = new ArrayList();
ClazzesService clazzService= (ClazzesService)ac.getBean("clazzesService");
Clazzes clazz=clazzService.findClazzById("59658fd4d724ccce5ee5cc5b");if(clazz!=null){for(int i=0;i<10;i++){
Student student= newStudent();
student.setAge(11);
student.setClazzes(clazz);
student.setEnterYear("2015");
student.setName("学生"+i);
studentdao.insert(student);
studentList.add(student);
}
clazz.setStudent(studentList);
clazzService.save(clazz);
}
}/*** 初始化一个班级
*@throwsException*/@Testpublic void InsertClazz() throwsException{
ClazzesService clazzService= (ClazzesService)ac.getBean("clazzesService");
Clazzes c= newClazzes();
c.setClassRoom("2014年1班");
c.setClassTeacher("Mrs zhang");
c.setStudent(newArrayList());
clazzService.insert(c);
}/*** 通过联合查询获取班级下的所有学生信息
*@throwsException*/@Testpublic void getAllClazz() throwsException{
ClazzesService clazzService= (ClazzesService)ac.getBean("clazzesService");
Clazzes c= clazzService.findClazzById("59658fd4d724ccce5ee5cc5b");for(Student s :c.getStudent()){
System.out.println("联合查询学生姓名:"+s.getName());
}
}/** 通过学生的id获取学生的班级*/@Testpublic void findAllStudent() throwsException{
StudentService s= (StudentService)ac.getBean("studentService");
Student ss= s.findUserById("59658831d724a1cb751c3ef8");//通过联合查询获取班级信息
System.out.println(ss.getClazzes().getClassRoom());
}/*** 删除学生的时候执行联合删除班级中的学生
*@throwsException*/@Testpublic void deleteStudent() throwsException{
StudentService s= (StudentService)ac.getBean("studentService");
ClazzesService clazzService= (ClazzesService)ac.getBean("clazzesService");
Student ss= s.findUserById("596591e1d7241f4590bddef5");
List list=ss.getClazzes().getStudent();
List listnew = new ArrayList();for(Student stu:list){if(!stu.getId().equals("596591e1d7241f4590bddef5")){
listnew.add(stu);
}
}
Clazzes c=ss.getClazzes();
c.setStudent(listnew);
clazzService.save(c);
s.remove(ss);
}
}