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

package com.java.web;

import java.util.List;

import org.mongodb.framework.pojo.GeneralBean;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document; @Document
public class Clazzes extends GeneralBean { /**
*
*/
private static final long serialVersionUID = -1151165767494158740L;
private String classRoom;
private String classTeacher;
@DBRef
private List<Student> student;
public String getClassRoom() {
return this.classRoom;
}
public void setClassRoom(String classRoom) {
this.classRoom = classRoom;
}
public String getClassTeacher() {
return this.classTeacher;
}
public void setClassTeacher(String classTeacher) {
this.classTeacher = classTeacher;
}
public List<Student> getStudent() {
return this.student;
}
public void setStudent(List<Student> student) {
this.student = student;
} }
package com.java.web;

import org.mongodb.framework.dao.GeneralDao;

public interface ClazzesDao  extends GeneralDao<Clazzes>{

}
package com.java.web;

import org.mongodb.framework.dao.GeneralDaoImpl;
import org.springframework.stereotype.Repository; @Repository
public class ClazzesDaoImpl extends GeneralDaoImpl<Clazzes> implements ClazzesDao{ @Override
protected Class<Clazzes> getEntityClass() {
// TODO Auto-generated method stub
return Clazzes.class;
} }
package com.java.web;

import org.mongodb.framework.service.GeneralServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service; import com.java.manage.pojo.User; @Service
public class ClazzesService extends GeneralServiceImpl<Clazzes> { @Autowired
private ClazzesDao clazzDao; /**
* 根据用户id查询用户
*
* @param id
* @return
* @throws Exception
*/
public Clazzes findClazzById(String id) throws Exception {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(id));
// User user= this.userDao.findOneById(id);
Clazzes clazz = this.clazzDao.findOneByQuery(query);
if (clazz != null)
return clazz;
else
return null;
} }
package com.java.web;

import org.mongodb.framework.pojo.GeneralBean;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document; @Document
public class Student extends GeneralBean { /**
*
*/
private static final long serialVersionUID = 5697238875408915428L;
/**
*
*/
private String name;
private int age;
private String enterYear;
@DBRef
private Clazzes clazzes;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getEnterYear() {
return this.enterYear;
}
public void setEnterYear(String enterYear) {
this.enterYear = enterYear;
}
public Clazzes getClazzes() {
return this.clazzes;
}
public void setClazzes(Clazzes clazzes) {
this.clazzes = clazzes;
} }
package com.java.web;

import org.mongodb.framework.dao.GeneralDao;

public interface StudentDao  extends GeneralDao<Student>{

}
package com.java.web;

import org.mongodb.framework.dao.GeneralDaoImpl;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDaoImpl extends GeneralDaoImpl<Student> implements StudentDao{ @Override
protected Class<Student> getEntityClass() {
// TODO Auto-generated method stub
return Student.class;
} }
package com.java.web;

import org.mongodb.framework.dao.GeneralDaoImpl;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDaoImpl extends GeneralDaoImpl<Student> implements StudentDao{ @Override
protected Class<Student> getEntityClass() {
// TODO Auto-generated method stub
return Student.class;
} }

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

package org.java.test;

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

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. python文件名和文件路径操作

    Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍"获得当前路径""获得 ...

  2. jquery.i18n.properties前端国际化解决方案“填坑日记”

    但现在的情况是老的项目并没有使用这类架构.说起国际化,博主几年前就做过,在MVC里面实现国际化有通用的解决方案,主要就是通过资源文件的方式定义多语言.最初接到这个任务,并没有太多顾虑,毕竟这种东西有很 ...

  3. uvalive 3029 City Game

    https://vjudge.net/problem/UVALive-3029 题意: 给出一个只含有F和R字母的矩阵,求出全部为F的面积最大的矩阵并且输出它的面积乘以3. 思路: 求面积最大的子矩阵 ...

  4. spring boot系列01--快速构建spring boot项目

    最近的项目用spring boot 框架 借此学习了一下 这里做一下总结记录 非常便利的一个框架 它的优缺点我就不在这背书了 想了解的可以自行度娘谷歌 说一下要写什么吧 其实还真不是很清楚,只是想记录 ...

  5. vue-resource传参数到后端,后端取不到数据的问题

    先上一段代码: this.$http.post('xxx',{Search_Text:this.search_text}).then(function(response){ // 响应成功回调 thi ...

  6. 云计算---OpenStack Neutron详解

    简介: neutron是openstack核心项目之一,提供云计算环境下的虚拟网络功能 OpenStack网络(neutron)管理OpenStack环境中所有虚拟网络基础设施(VNI),物理网络基础 ...

  7. PHP的ntohl网络字节序函数及相关知识

    PHP与C服务器的socket通信,在做数据转换的时候,PHP没有提供对应将网络字节序和机器字节序相互转换的程序,但是根据函数的意义,我们可以做相应的转换来实现这一函数: function ntohl ...

  8. Python自学笔记-lambda函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 匿名函数 通过 ...

  9. es6零基础学习之构建脚本(二)

    编译器打开你的es6项目 首先:创建我们的第一个脚本,tasks/util/args.js      在文件里面要先引入一个包,处理命令行参数 import yargs from 'yargs'; / ...

  10. Sqlite常用sql语句

    sqlite常用sql语句 --返回UTC时间 select CURRENT_TIMESTAMP; --返回本地时间 select datetime(CURRENT_TIMESTAMP,'localt ...