Mongodb使用联合查询的重点需要添加@DBref  这样的话不会将整个文档保存,只会保存关联集合的id

  1. package com.java.web;
  2.  
  3. import java.util.List;
  4.  
  5. import org.mongodb.framework.pojo.GeneralBean;
  6. import org.springframework.data.mongodb.core.mapping.DBRef;
  7. import org.springframework.data.mongodb.core.mapping.Document;
  8.  
  9. @Document
  10. public class Clazzes extends GeneralBean {
  11.  
  12. /**
  13. *
  14. */
  15. private static final long serialVersionUID = -1151165767494158740L;
  16. private String classRoom;
  17. private String classTeacher;
  18. @DBRef
  19. private List<Student> student;
  20. public String getClassRoom() {
  21. return this.classRoom;
  22. }
  23. public void setClassRoom(String classRoom) {
  24. this.classRoom = classRoom;
  25. }
  26. public String getClassTeacher() {
  27. return this.classTeacher;
  28. }
  29. public void setClassTeacher(String classTeacher) {
  30. this.classTeacher = classTeacher;
  31. }
  32. public List<Student> getStudent() {
  33. return this.student;
  34. }
  35. public void setStudent(List<Student> student) {
  36. this.student = student;
  37. }
  38.  
  39. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.dao.GeneralDao;
  4.  
  5. public interface ClazzesDao extends GeneralDao<Clazzes>{
  6.  
  7. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.dao.GeneralDaoImpl;
  4. import org.springframework.stereotype.Repository;
  5.  
  6. @Repository
  7. public class ClazzesDaoImpl extends GeneralDaoImpl<Clazzes> implements ClazzesDao{
  8.  
  9. @Override
  10. protected Class<Clazzes> getEntityClass() {
  11. // TODO Auto-generated method stub
  12. return Clazzes.class;
  13. }
  14.  
  15. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.service.GeneralServiceImpl;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.mongodb.core.query.Criteria;
  6. import org.springframework.data.mongodb.core.query.Query;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import com.java.manage.pojo.User;
  10.  
  11. @Service
  12. public class ClazzesService extends GeneralServiceImpl<Clazzes> {
  13.  
  14. @Autowired
  15. private ClazzesDao clazzDao;
  16.  
  17. /**
  18. * 根据用户id查询用户
  19. *
  20. * @param id
  21. * @return
  22. * @throws Exception
  23. */
  24. public Clazzes findClazzById(String id) throws Exception {
  25. Query query = new Query();
  26. query.addCriteria(Criteria.where("_id").is(id));
  27. // User user= this.userDao.findOneById(id);
  28. Clazzes clazz = this.clazzDao.findOneByQuery(query);
  29. if (clazz != null)
  30. return clazz;
  31. else
  32. return null;
  33. }
  34.  
  35. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.pojo.GeneralBean;
  4. import org.springframework.data.mongodb.core.mapping.DBRef;
  5. import org.springframework.data.mongodb.core.mapping.Document;
  6.  
  7. @Document
  8. public class Student extends GeneralBean {
  9.  
  10. /**
  11. *
  12. */
  13. private static final long serialVersionUID = 5697238875408915428L;
  14. /**
  15. *
  16. */
  17. private String name;
  18. private int age;
  19. private String enterYear;
  20. @DBRef
  21. private Clazzes clazzes;
  22. public String getName() {
  23. return this.name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public int getAge() {
  29. return this.age;
  30. }
  31. public void setAge(int age) {
  32. this.age = age;
  33. }
  34. public String getEnterYear() {
  35. return this.enterYear;
  36. }
  37. public void setEnterYear(String enterYear) {
  38. this.enterYear = enterYear;
  39. }
  40. public Clazzes getClazzes() {
  41. return this.clazzes;
  42. }
  43. public void setClazzes(Clazzes clazzes) {
  44. this.clazzes = clazzes;
  45. }
  46.  
  47. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.dao.GeneralDao;
  4.  
  5. public interface StudentDao extends GeneralDao<Student>{
  6.  
  7. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.dao.GeneralDaoImpl;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public class StudentDaoImpl extends GeneralDaoImpl<Student> implements StudentDao{
  7.  
  8. @Override
  9. protected Class<Student> getEntityClass() {
  10. // TODO Auto-generated method stub
  11. return Student.class;
  12. }
  13.  
  14. }
  1. package com.java.web;
  2.  
  3. import org.mongodb.framework.dao.GeneralDaoImpl;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public class StudentDaoImpl extends GeneralDaoImpl<Student> implements StudentDao{
  7.  
  8. @Override
  9. protected Class<Student> getEntityClass() {
  10. // TODO Auto-generated method stub
  11. return Student.class;
  12. }
  13.  
  14. }

上面贴的都是基本的代码,下面进行junit测试

  1. package org.java.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;
  10. import org.springframework.transaction.annotation.Transactional;
  11.  
  12. import com.java.web.Clazzes;
  13. import com.java.web.ClazzesService;
  14. import com.java.web.Student;
  15. import com.java.web.StudentDao;
  16. import com.java.web.StudentService;
  17.  
  18. public class TestInertStudentClass {
  19.  
  20. ApplicationContext ac=null;
  21.  
  22. @Before
  23. public void befort(){
  24. ac=new ClassPathXmlApplicationContext(new String[]{"application-config.xml","dispatcher-servlet.xml","dispatcher-shiro.xml"});
  25. }
  26.  
  27. /**
  28. * 添加学生并且绑定班级
  29. * @throws Exception
  30. */
  31. @Test
  32. public void InsertStudent() throws Exception {
  33. StudentDao studentdao=(StudentDao) ac.getBean("studentDaoImpl");
  34. List<Student> studentList = new ArrayList<Student>();
  35. ClazzesService clazzService = (ClazzesService)ac.getBean("clazzesService");
  36. Clazzes clazz =clazzService.findClazzById("59658fd4d724ccce5ee5cc5b");
  37. if(clazz!=null){
  38. for(int i=0;i<10;i++){
  39. Student student = new Student();
  40. student.setAge(11);
  41. student.setClazzes(clazz);
  42. student.setEnterYear("2015");
  43. student.setName("学生"+i);
  44. studentdao.insert(student);
  45. studentList.add(student);
  46. }
  47. clazz.setStudent(studentList);
  48. clazzService.save(clazz);
  49. }
  50. }
  51.  
  52. /**
  53. * 初始化一个班级
  54. * @throws Exception
  55. */
  56. @Test
  57. public void InsertClazz() throws Exception{
  58. ClazzesService clazzService = (ClazzesService)ac.getBean("clazzesService");
  59. Clazzes c = new Clazzes();
  60. c.setClassRoom("2014年1班");
  61. c.setClassTeacher("Mrs zhang");
  62. c.setStudent(new ArrayList());
  63. clazzService.insert(c);
  64.  
  65. }
  66.  
  67. /**
  68. * 通过联合查询获取班级下的所有学生信息
  69. * @throws Exception
  70. */
  71. @Test
  72. public void getAllClazz() throws Exception{
  73. ClazzesService clazzService = (ClazzesService)ac.getBean("clazzesService");
  74. Clazzes c = clazzService.findClazzById("59658fd4d724ccce5ee5cc5b");
  75. for(Student s :c.getStudent()){
  76. System.out.println("联合查询学生姓名:"+s.getName());
  77. }
  78.  
  79. }
  80.  
  81. /*
  82. * 通过学生的id获取学生的班级
  83. */
  84. @Test
  85. public void findAllStudent() throws Exception{
  86. StudentService s = (StudentService)ac.getBean("studentService");
  87. Student ss = s.findUserById("59658831d724a1cb751c3ef8");
  88. //通过联合查询获取班级信息
  89. System.out.println(ss.getClazzes().getClassRoom());
  90. }
  91.  
  92. /**
  93. * 删除学生的时候执行联合删除班级中的学生
  94. * @throws Exception
  95. */
  96. @Test
  97. public void deleteStudent() throws Exception{
  98. StudentService s = (StudentService)ac.getBean("studentService");
  99. ClazzesService clazzService = (ClazzesService)ac.getBean("clazzesService");
  100. Student ss = s.findUserById("596591e1d7241f4590bddef5");
  101. List<Student> list= ss.getClazzes().getStudent();
  102. List<Student> listnew = new ArrayList<Student>();
  103. for(Student stu:list){
  104. if(!stu.getId().equals("596591e1d7241f4590bddef5")){
  105. listnew.add(stu);
  106. }
  107. }
  108.  
  109. Clazzes c = ss.getClazzes();
  110. c.setStudent(listnew);
  111. clazzService.save(c);
  112. s.remove(ss);
  113.  
  114. }
  115.  
  116. }

Mongodb联合查询的更多相关文章

  1. MongoDB联合查询 -摘自网络

    1.简单手工关联 首先将结果查询出来放到一个变量里面,然后再查询 u = db.user.findOne({author:"wangwenlong"}); for(var p = ...

  2. Spring DATA MongoDB @DBref查询,or和and联合查询

    @DBref文档关联,在按该类型查询的时候,在字段名后加上关联表的字段名即可,如: Criteria.where("bloggroup.$id"), $id代表关联表的oid字段. ...

  3. mongodb中查询返回指定字段

    mongodb中查询返回指定字段   在写vue项目调用接口获取数据的时候,比如新闻列表页我只需要显示新闻标题和发表时间,点击每条新闻进入详情页的时候才会需要摘要.新闻内容等关于此条新闻的所有字段.  ...

  4. TODO:MongoDB的查询更新删除总结

    TODO:MongoDB的查询更新删除总结 常用查询,条件操作符查询,< .<=.>.>=.!= 对应 MongoDB的查询操作符是$lt.$lte.$gt.$gte.$ne ...

  5. SQL联合查询:子表任一记录与主表联合查询

    今天有网友群里提了这样一个关于SQL联合查询的需求: 一.有热心网友的方案: 二.我的方案: select * from ( select a.*,(select top 1 Id from B as ...

  6. SQL 语句与性能之联合查询和联合分类查询

    select * from t1 left join t2 on t2.sysno =t1.ASysNo left join t3 on t3.sysno =t2.ASysNo left join t ...

  7. myBatis中 collection 或 association 联合查询 中column 传入多个参数值

    下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap"  type="com.maidan.daas.entit ...

  8. EF联合查询,如何设置条件过滤从表数据

    最近在使用EF进行联合查询过程中,遇到了一件不开心的事情. 已禁用懒加载 var post = await _repository.GetMyPostById(blogId, postId).AsNo ...

  9. 【转】Mysql联合查询union和union all的使用介绍

    Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...

随机推荐

  1. foreach循环中为什么不要进行remove/add操作

    先来看一段代码,摘自阿里巴巴的java开发手册 List<String> a = new ArrayList<String>(); a.add("1"); ...

  2. Linux入门之常用命令(12) mount

    查看Linux所有设备 cd  /dev ls -l sd*  //分区 查看Linux磁盘 fdisk -lu 挂载 一般挂载至/mnt /media mount /dev/sda5   /mnt/ ...

  3. Linux入门之常用命令(8)上传下载

    [什么是rz/sz (lsz/lrz)]  简单说就是,可以很方便地用这两个sz/rz工具,实现Linux下和Windows之间的文件传输(发送和接收),速度大概为10KB/s,适合中小文件.rz/s ...

  4. 郑厂长系列故事——排兵布阵 hdu4539(状态压缩DP)

    郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  5. BZOJ-1192-[HNOI2006]鬼谷子的钱袋

    Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一 ...

  6. 简单Elixir游戏服务器开篇

    以前的Elixir游戏服设计系列种种原因没有完成. 后来虽然用Elixir + riak 完成了一个麻将的初始版本,可惜公司也挂了. 现在到新公司,比较空闲,想着像完成一个心愿一样,还是重启下吧(希望 ...

  7. ASP.NET没有魔法——目录

    ASP.NET没有魔法——开篇-用VS创建一个ASP.NET Web程序 ASP.NET没有魔法——为什么使用ASP.NET ASP.NET没有魔法——第一个ASP.NET应用<MyBlog&g ...

  8. 入门VMware Workstation下的Debian学习之Vim简单使用(三)

    什么是Vim? Vim具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性,方便程序设计. Vim是从vi发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广 ...

  9. 使用MVVM减少控制器代码实战(减少56%)

    减少比例= (360(原来的行数)-159(瘦身后的行数))/360 = 56% 父类 MVC 和MVVM 前后基本不动 父类主要完成如下三个功能: 1)功能:MJRefrsh +上拉下拉没有更多数据 ...

  10. 【原创】修复ios输入框获取焦点时不支持fixed的bug

    前些日子,做了一个手机站的项目,有一个页面是这样的, 有一个固定(position:fixed)的头部和底部导航,中间是一些表单内容,没啥特别的.但是到了ios中,正常滚动页面没有问题,一旦触发了文本 ...