java mongodb 关联查询_Mongodb联合查询

该博客展示了如何在Java应用程序中使用MongoDB进行关联查询。通过Spring框架,作者创建了测试类`TestInertStudentClass`,进行了一系列操作,如插入班级和学生,通过联合查询获取班级下的所有学生信息,根据学生ID获取班级信息,以及在删除学生时更新班级中的学生列表。整个过程详细说明了Java与MongoDB结合处理关联数据的方法。
摘要由CSDN通过智能技术生成

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值