mongoTemplate增删改查

mongoTemplate简单用法(增删改查)

1、在实体类MgUser.java中引入注解表明转换方式


@Data

//需要与mongo中对象(文档)名一致

public class MgUser {

    @Id

    private ObjectId _id;    //插入mongo时会自动生成_id,如果不加这个字段则会把id属性当成_id

    @Field

    private Long id;

    private String nikename;

    private String phone;


          /**


            *提供set/get和toString方法


            */


          }


2、配置MongoTemplate

     在.xml中添加bean如下


      

   


   

       

   


3、将MongoTemplate注入DAO,并调用其相关方法进行CURD(创建(Create)、更新(Update)、读取(Read)和删除(Delete))操作


    public class TestMonoTemplate{


     @Autowired


       private MongoTemplate mongoTemplate;


      @Test


       public void testCURD(){


           //测试增删改查操作(此处简化举例)


            mongoTemplate.save();//添加


            mongoTemplate.find();  //查询操作


            mongoTemplate.findOne();//单个结果查询操作


            mongoTemplate.updateFirst();//进行第一条符合要求的数据更新


           mongoTemplate.updateMulti();//进行更新多行数据


           mongoTemplate.remove();//进行数据删除


       }


    }


分页时查找数量:

1
2
3
4
5
6
7
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 );
     }

  分页:

1
2
3
4
5
6
7
8
9
10
11
12
public  List 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 );
    }

  修改数量:

1
2
3
4
5
6
7
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 );
    }

  模糊查询:

1
2
3
4
5
6
7
8
public  List 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()));
    }

  一般查询:

1
2
3
4
5
6
7
8
public  List 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 );
     }

  

1
2
3
4
5
6
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 );
    }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
   }

你可能感兴趣的:(数据库)