spring boot集成mongodb的增删改查的示例代码

网友投稿 346 2023-01-25

spring boot集成mongodb的增删改查的示例代码

添加依赖

org.springframework.boot

spring-boot-starter-data-mongodb

properties配置

spring.data.mongodb.host=127.0.0.1

spring.data.mongodb.port=27017

spring.data.mongodb.database=enterprise_dc_db

spring.data.mongodb.username=enterprise_dc_dba

spring.data.mongodb.password=123456

server.port=8081

创建实体类

@Document为mongodb集合名

package com.example.springbootmongodb.domian;

import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "mydb")

public class Users {

private String userId;

private String name;

private Integer age;

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

controller

package com.example.springbootmongodb.controller;

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;

}

}

package com.example.springbootmongodb.controller;

import com.example.springbootmongodb.domian.Users;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.mongodb.core.query.Update;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/mongo")

@RestController

public class UserController {

@Autowired

private MongoTemplate mongoTemplate;

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public String hello(){

return "hello";

}

/**

* 新增

*/

@ResponseBoXDkaATody

@RequestMapping(value = "/insert", method = RequestMethod.GET)

public ResultObject insert(){oXDkaAT

Users user = new Users();

user.setUserId("1002");

user.setName("eric");

user.setAge(18);

mongoTemplate.insert(user);

return new ResultObject(200);

}

/**删除

*

* @return

*/

@ResponseBody

@RequestMapping("/delete")

public ResultObject delete(){

Query query = Query.query(Criteria.where("userId").is("1001"));

mongoTemplate.remove(query,"mydb");

return new ResultObject(200);

}

/**

* 删除

* @return

*/

@ResponseBody

@RequestMapping("/update")

public ResultObject update(){

Query query = Query.query(Criteria.where("userId").is("1002"));

Update update = new Update();

update.set("name","name1");

update.set("age",18);

mongoTemplate.updateFirst(query,update,"mydb");

return new ResultObject(200);

}

/**

* 查询

* @return

*/

@ResponseBody

@RequestMapping("/query")

public ResultObject query(){

Query query = Query.query(Criteria.where("userId").is("1001"));

List list = mongoTemplate.find(query,Users.class);

return new ResultObject(200,list);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:免费api汇率转换代码(在线汇率转换计算器)
下一篇:智能硬件开放api接口(硬件API)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~