SpringBoot 整合MongoDB基本的增删改查

SpringBoot 整合MongoDB基本的增删改查

导入maven

 <dependency>
     <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>

配置yml

spring: 
  data:
    mongodb:
      database: funeral
      host: 你的mongodb地址
      port: 27017

创建需要用到的实体类

/**
 1. 接收mongodb
 */

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;


@Data
@Document(collection = "memorial_hall_message")//表名
public class MemorialHallSend implements Serializable {

    private static final long serialVersionUID = 8983888591819506459L;

    @Id
    private String  id;//id

    @Field("sentence")
    private String sentence;//内容

    @Field("create_time")
    private String createTime;//创建时间
  }

1. 查询数据

全查询

 @Autowired
    private MongoTemplate mongoTemplate;


    @GetMapping("/test")
    @ResponseBody
    public void test(){
       List<MemorialHallSend> memorialHallSends = mongoTemplate.findAll(MemorialHallSend.class);
       for (MemorialHallSend hallSend:memorialHallSends ) {
           System.out.println(hallSend);
       }

   }

条件全查询
引用静态的Criteria.where的把多个条件组合在一起,多个用and
例如: Criteria criteria = Criteria.where(“info_id”).is(info_id).and(“id”).is(id)

	@Autowired
    private MongoTemplate mongoTemplate;


    @GetMapping("/test")
    @ResponseBody
    public void test(String info_id){
        Query query = new Query();
        Criteria criteria = Criteria.where("info_id").is(info_id);
        query.addCriteria(criteria);

        List<MemorialHallSend> memorialHallSends = mongoTemplate.find(query, MemorialHallSend.class);
       for (MemorialHallSend hallSend:memorialHallSends ) {
           System.out.println(hallSend);
       }

   }

查询单条数据(findOne)

@Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("/test")
    @ResponseBody
    public void test(String info_id){
        Query query = new Query();
        Criteria criteria = Criteria.where("info_id").is(info_id);
        query.addCriteria(criteria);

        MemorialHallSend one = mongoTemplate.findOne(query, MemorialHallSend.class);
        System.out.println(one);

   }

全查询并且排序
query.with(Sort.by(Sort.Direction.DESC,“createTime”));//根据时间倒叙

 @Autowired
    private MongoTemplate mongoTemplate;


    @GetMapping("/test")
    @ResponseBody
    public void test(String info_id){
        Query query = new Query();
        Criteria criteria = Criteria.where("info_id").is(info_id);
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));//根据时间倒叙

        List<MemorialHallSend> hallSend = mongoTemplate.find(query, MemorialHallSend.class);
        for (MemorialHallSend hallSend1:hallSend ) {
            System.out.println(hallSend1);
        }

   }

2. 插入数据

@Autowired
    private MongoTemplate mongoTemplate;


    @GetMapping("/test")
    @ResponseBody
    public void test(){
        MemorialHallSend hallSend = new MemorialHallSend();
        hallSend.setInfoId("10086");
        hallSend.setSentence("我是内容");
        hallSend.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));

        MemorialHallSend save = mongoTemplate.save(hallSend);
        System.out.println(save);
   }

3. 修改数据

修改一条数据的一个字段
根据info_id修改sentence

@Autowired
    private MongoTemplate mongoTemplate;


    @GetMapping("/test")
    @ResponseBody
    public void test(String info_id,String sentence){
        //更改一条数据的一个字段
        Query query = Query.query(Criteria.where("info_id").is(info_id));
        Update update = Update.update("sentence", sentence);
        //参数1:查询条件  参数2:修改  参数3:表名称(可以传对应集合的对象名)
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, MemorialHallSend.class);
        System.out.println(updateResult);
    }

修改一条数据的多个字段

@Autowired
private MongoTemplate mongoTemplate;


@GetMapping("/test")
@ResponseBody
public void test(String id){
    Query query = Query.query(Criteria.where("id").is(id));
    Update update = new Update();
    update.set("sentence", "hahahaha");
    update.set("info_id", "10010");
    //参数1:查询条件  参数2:修改  参数3:表名称(也可以是具体集合名)
    UpdateResult updateResult = mongoTemplate.updateFirst(query, update, MemorialHallSend.class);
    System.out.println(updateResult);
}

4. 删除数据

@Autowired
    private MongoTemplate mongoTemplate;
    

    /**
     * 通过id内容
     */
    @GetMapping("/deleteMemorialHallSend")
    @ResponseBody
    public ResultData deleteMemorialHallSend(String id){

            Query query = new Query();
            Criteria criteria = Criteria.where("id").is(id);
            query.addCriteria(criteria);
            DeleteResult remove = mongoTemplate.remove(query, MemorialHallSend.class);
         

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值