mongoTemplate简单用法(增删改查)

分页时查找数量:

public long countSample(String id) {
        Query query = new Query();
        if (StringUtil.hasText(id))
            query.addCriteria(Criteria.where("creator").is(id));

        return mongoTemplate.count(query, Sample.class);
    }

  分页:

 public List<Sample> searchAllSample(String id, PageTag page) {
        Query query = new Query();
        query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "update_date")));
        int skip = (page.getCurrentPage() - 1) * page.getPageSize();
        if (StringUtil.hasText(id)) {
            Criteria criteria = Criteria.where("creator").is(id);
            query.addCriteria(criteria);
        }
        query.skip(skip);// 从那条记录开始
        query.limit(page.getPageSize());// 取多少条记录
        return mongoTemplate.find(query, Sample.class);
    }

  修改数量:

 public void modifyHitNumByNo(String no) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sap_no").is(no));
        Update update = new Update();
        update.inc("hit_num", 1);
        mongoTemplate.updateFirst(query, update, Project.class);
    }

  模糊查询:

 public List<Sample> findSamples(String memberId, SelectVo selectVo) {
        Criteria criteria = Criteria.where("creator").is(memberId);
        if (StringUtil.hasText(selectVo.getName())) {
            Pattern pattern = Pattern.compile("^.*" + selectVo.getName() + ".*$", Pattern.CASE_INSENSITIVE);
            criteria.orOperator(Criteria.where("projectNo").regex(pattern), Criteria.where("name").regex(pattern));
        }
        return findPageList(Query.query(criteria), Sample.class, new PageTag(selectVo.getPage(), selectVo.getPageSize()));
    }

  一般查询:

public List<Project> getProjects(String creator) {
        Query query = new Query();
        if (StringUtil.hasText(creator)) {
            Criteria criteria = Criteria.where("creator").is(creator);
            query.addCriteria(criteria);
        }
        return mongoTemplate.find(query, Project.class);
    }

  

 public Experiment getExperimentByExpNo(String expNo) {
        Query query = new Query();
        Criteria criteria = Criteria.where("exp_no").is(expNo);
        query.addCriteria(criteria);
        return mongoTemplate.findOne(query, Experiment.class);
    }

  

  public void saveOrUpdateExperiment(Experiment experiment) {

        if (StringUtil.hasText(experiment.getExpNo())) {
            Query query = new Query();
            Criteria criteria = Criteria.where("exp_no").is(experiment.getExpNo());
            query.addCriteria(criteria);
            Experiment exp = mongoTemplate.findAndRemove(query, Experiment.class);
            experiment.setCreator(exp.getCreator());
            experiment.setCreateDate(exp.getCreateDate());
        }

        mongoTemplate.save(experiment);
    }

  

posted @ 2016-11-18 10:14  xfma  阅读(36482)  评论(0编辑  收藏  举报