1. 程式人生 > >18.springboot整合mongodb

18.springboot整合mongodb

1.window安裝MongoDB

http://baijiahao.baidu.com/s?id=1601512248926547477&wfr=spider&for=pc

2.使用Navicat for mongodb連線

http://www.ddooo.com/softdown/130308.htm

3.springboot整合MongoDB

參考:

https://www.cnblogs.com/mao2080/p/9571699.html

3.1 搭建專案

 

 

 

 

 

3.2 配置連線MongoDB資料庫

#庫id
spring.data.mongodb.host=127.0.0.1
#MongoDB埠
spring.data.mongodb.port=27017
#資料庫
spring.data.mongodb.database=mongodb_table

3.3 controller 層

 

3.4 domain

 

 

3.5 工具類

package cn.ma.mongdb_springboot.utils;

public class ResultObject {
    private int code;
    private String desc;
    private Object data;
    public ResultObject() {}
    public ResultObject(int code) {
        this.code = code;
    }
    public ResultObject(int code, Object data) {
        this.code = code;
        this.data = data;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
}

 

3.6 測試

3.6.1 插入資料

/**
 * 1.向mongodb中新增一條資料
 * http://localhost:8080/insert
 * @Date 16:54 2019/6/29
 * @Param []
 * @return cn.ma.mongodb.utils.ResultObject
 **/
@RequestMapping(value = "/insert")
@ResponseBody
public ResultObject insert(@RequestBody Student student) {
    this.mongoTemplate.insert(student);
    return new ResultObject(HttpServletResponse.SC_OK);
}

 

 

 

 

3.6.2  修改 

 

 

 

3.6.3 刪除

 

 

3.6.4 查詢

3.6.4.1 根據id查詢