• MongDB之各种查询操作


    接口IMongDaoFind:

    package com.net.test.mongdb.dao;
    
    public interface IMongDaoFind {
        
        public void findUsers();
        
        public void findUserByName(String name);
         
        public void findUserLikeName(String name);
        
        public void findUserPs();
        
        public void findUserAndOperator();
        
        public void findUserOrOperator();
        
        public void findUserOrOperator2();
    }

    实现类MongDaoFindImp:

    package com.net.test.mongdb.dao.imp;
     
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.stereotype.Repository;
    
    import com.net.test.mongdb.dao.IMongDaoFind;
    import com.net.test.mongdb.entity.User;
    import com.net.test.mongdb.entity.UserP;
    import com.net.test.mongdb.entity.UserPsg;
     
    
    /**
     * @author ***
     * @Time:2017年8月4日 上午10:40:33
     * @version 1.0  
     * Function: TODO
     */
    @Repository 
    public class MongDaoFindImp implements IMongDaoFind{
        
        @Resource
        protected MongoTemplate mongoTemplate;
     
        /**
         * @description 查询所有用户信息
         */
        @Override
        public void findUsers()
        {
            List<User> list = mongoTemplate.findAll(User.class);
            for(User user : list)
            {
                System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex());
            }
        }
        
        /**
         * @description 根据用户名,单条查询用户信息.如果有重复名称,默认查询第一条
         * @param name
         */
        public void findUserByName(String name)
        {    
            Query query = Query.query(Criteria.where("name").is(name));
            User user = mongoTemplate.findOne(query,User.class);
            System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex());
        }
        
         
        /**
         * @description 模糊查询
         * @param name
         */
        @Override
        public void findUserLikeName(String name) 
        {
            Query query = new Query();
            query.addCriteria(Criteria.where("name").regex(name));
            
            List<User> list = mongoTemplate.find(query, User.class);
            for(User user : list)
            {
                System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex());
            }
        }
        
        /**
         * @description 表名与pojo不相同进行映射
         */
        @Override
        public void findUserPs() 
        {
            List<UserP> list = mongoTemplate.findAll(UserP.class, "user");
            for(UserP user : list)
            {
                System.out.println("name :" + user.getName() + " , age :" + user.getAge() + 
                        " , sex :" + user.getSex() + " , hobby :" + user.getHobby());
            }
        }
        
        /**
         * @description 测试多条件进行查询,测试and连接符 
         */
        @Override
        public void findUserAndOperator() {
    
            Query query = new Query();
            Criteria c  = new Criteria();
            query.addCriteria(c.andOperator(Criteria.where("name").regex("车"),
                    Criteria.where("age").is(18)));
         
            List<User> list = mongoTemplate.find(query, User.class);
            for(User user : list)
            {
                //基础信息
                String name = user.getName();
                int age = user.getAge();
                String sex = user.getSex();
                
                //内置对象
                UserPsg psg = user.getPsg();
    
                //内置数组
                ArrayList<String> listHobby = (ArrayList<String>) user.getHobby();
                StringBuffer bf = new StringBuffer();
                if(null != listHobby && listHobby.size() > 0)
                {
                    for(String hobby : listHobby)
                    {
                        bf.append(hobby + ",");
                    }
                }else
                {
                    bf.append("");
                }
                
                System.out.println(
                        "name : " + name +
                        ", age : " + age + 
                        ", sex : " + sex +
                        ", hobby : " + bf.toString() +
                        ", UserPsg : " + psg
                        );
            }
        }
        
        /**
         * @description 测试多条件进行查询,测试平行并列or
         */
        @Override
        public void findUserOrOperator() 
        {
            Query query = new Query();
            Criteria c  = new Criteria();
            query.addCriteria(c.orOperator(Criteria.where("name").regex("车"),
                    Criteria.where("age").is(10)));
         
            List<User> list = mongoTemplate.find(query, User.class);
            for(User user : list)
            {
                //基础信息
                String name = user.getName();
                int age = user.getAge();
                String sex = user.getSex();
                
                //内置对象
                UserPsg psg = user.getPsg();
    
                //内置数组
                ArrayList<String> listHobby = (ArrayList<String>) user.getHobby();
                StringBuffer bf = new StringBuffer();
                if(null != listHobby && listHobby.size() > 0)
                {
                    for(String hobby : listHobby)
                    {
                        bf.append(hobby + ",");
                    }
                }else
                {
                    bf.append("");
                }
                
                System.out.println(
                        "name : " + name +
                        ", age : " + age + 
                        ", sex : " + sex +
                        ", hobby : " + bf.toString() +
                        ", UserPsg : " + psg
                        );
            }
        }
        
        /**
         * @description    测试多条件进行查询,测试包含关系or连接符,此处的or和and有些相似
         */
        @Override
        public void findUserOrOperator2() 
        {
            Query query = new Query();
            query.addCriteria(Criteria.where("name").regex("车").orOperator(Criteria.where("age").is(19)));
         
            List<User> list = mongoTemplate.find(query, User.class);
            for(User user : list)
            {
                //基础信息
                String name = user.getName();
                int age = user.getAge();
                String sex = user.getSex();
                
                //内置对象
                UserPsg psg = user.getPsg();
    
                //内置数组
                ArrayList<String> listHobby = (ArrayList<String>) user.getHobby();
                StringBuffer bf = new StringBuffer();
                if(null != listHobby && listHobby.size() > 0)
                {
                    for(String hobby : listHobby)
                    {
                        bf.append(hobby + ",");
                    }
                }else
                {
                    bf.append("");
                }
                
                System.out.println(
                        "name : " + name +
                        ", age : " + age + 
                        ", sex : " + sex +
                        ", hobby : " + bf.toString() +
                        ", UserPsg : " + psg
                        );
            }
        }
    }    
  • 相关阅读:
    Windows Phone 7之初体验(三.开发问答(转发))
    Windows phone 7 之初体验(一.安装Windows phone 7 sdk)
    python 程序的性能分析优化(huffman编码程序性能分析的一个小结论)
    图的深度优先遍历,及常见的扩展算法
    递归回溯与迭代回溯算法框架,打印在n个数字中取k个数字的所有可能
    python 实现的范式huffman压缩,解压缩
    <读书笔记> Thinking in python (Python 设计模式) 3. Proxy and State模式
    二分查找极其变形算法
    <读书笔记> Thinking in python (Python 设计模式) 1. Singlton的c++与python的实现
    <转载>openmesh文档的非专业翻译by kidux(学习generative programming非常好的库)
  • 原文地址:https://www.cnblogs.com/chen1-kerr/p/7326748.html
Copyright © 2020-2023  润新知