1. 程式人生 > >MongoDB之SpringDataMongoDB操作

MongoDB之SpringDataMongoDB操作

1.建立工程引入依賴

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

2.建立application.yml

server:
  port: 9006
spring:
  application:
    name: spit #指定服務名
  data:
    mongodb:
      host: 10.211.55.28
      database: spitdb

3.建立啟動類

package com.xdp.spit;

import com.xdp.common.utils.IdWorker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpitApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpitApplication.class, args);
    }

    @Bean
    public IdWorker idWorkker() {
        return new IdWorker(1, 1);
    }
}

4.建立實體類

package com.xdp.spit.entity;

import org.springframework.data.annotation.Id;

import java.io.Serializable;
import java.util.Date;

public class Spit implements Serializable {
    @Id
    private String _id;
    private String content;
    private Date publishtime;
    private String userid;
    private String nickname;
    private Integer visits;
    private Integer thumbup;
    private Integer share;
    private Integer comment;
    private String state;
    private String parentid;
    //get和set方法省略
}

5.DAO

package com.xdp.spit.dao;

import com.xdp.spit.entity.Spit;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface SpitDao extends MongoRepository<Spit,String> {
}

6.Service

package com.xdp.spit.service;

import com.xdp.common.utils.IdWorker;
import com.xdp.spit.dao.SpitDao;
import com.xdp.spit.entity.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查詢全部記錄
     * @return
     */
    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    /**
     * 根據主鍵查詢實體
     * @param id
     * @return
     */
    public Spit findById(String id){
        return spitDao.findById(id).get();
    }

    /**
     * 增加
     * @param spit
     */
    public void save(Spit spit){
        //生成分散式ID
        String id = idWorker.nextId() + "";
        spit.set_id(id);
        spitDao.save(spit);
    }
    /**
     * 修改
     * @param spit
     */
    public void update(Spit spit){
        spitDao.save(spit);
    }
    /**
     * 刪除
     * @param id
     */
    public void deleteById(String id){
        spitDao.deleteById(id);
    }

}

7.Controller

package com.xdp.spit.controller;

import com.xdp.common.entity.Result;
import com.xdp.common.entity.StatusCode;
import com.xdp.spit.entity.Spit;
import com.xdp.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin//允許跨域訪問
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查詢成功