• Mongodb联合查询


    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);
        
        
        }
       
    }
  • 相关阅读:
    卷积神经网络入门(1) 识别猫狗
    lumen 获得当前uri 如/xxx/{id}
    React ES5 (createClass) 和 ES6 (class)
    lumen 单元测试
    mysql 高级语法手记
    react手记(componentWillMount,componentDidMount等)
    lumen 事件
    PDO drivers no value in Windows
    BindingNavigator操作DatagridView的数据
    <input type="hidden" id="haha" name="wang" value="xiaodong" />
  • 原文地址:https://www.cnblogs.com/fliay/p/7154837.html
Copyright © 2020-2023  润新知