飞道的博客

spring boot集成mongodb的增删改查

298人阅读  评论(0)

添加依赖


  
  1. <dependency>
  2. <groupId>org.springframework.boot </groupId>
  3. <artifactId>spring-boot-starter-data-mongodb </artifactId>
  4. </dependency>

properties配置


  
  1. spring.data.mongodb.host= 127.0. 0.1
  2. spring.data.mongodb.port= 27017
  3. spring.data.mongodb.database=enterprise_dc_db
  4. spring.data.mongodb.username=enterprise_dc_dba
  5. spring.data.mongodb.password= 123456
  6. server.port= 8081
 

创建实体类

@Document为mongodb集合名


  
  1. package com.example.springbootmongodb.domian;
  2. import org.springframework.data.mongodb.core.mapping.Document;
  3. @Document(collection = "mydb")
  4. public class Users {
  5. private String userId;
  6. private String name;
  7. private Integer age;
  8. public String getUserId() {
  9. return userId;
  10. }
  11. public void setUserId(String userId) {
  12. this.userId = userId;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public Integer getAge() {
  21. return age;
  22. }
  23. public void setAge(Integer age) {
  24. this.age = age;
  25. }
  26. }
 

controller


  
  1. package com.example.springbootmongodb.controller;
  2. public class ResultObject {
  3. private int code;
  4. private String desc;
  5. private Object data;
  6. public ResultObject() {
  7. }
  8. public ResultObject(int code) {
  9. this.code = code;
  10. }
  11. public ResultObject(int code, Object data) {
  12. this.code = code;
  13. this. data = data;
  14. }
  15. public Object getData() {
  16. return data;
  17. }
  18. public void setData(Object data) {
  19. this. data = data;
  20. }
  21. public int getCode() {
  22. return code;
  23. }
  24. public void setCode(int code) {
  25. this.code = code;
  26. }
  27. public String getDesc() {
  28. return desc;
  29. }
  30. public void setDesc(String desc) {
  31. this.desc = desc;
  32. }
  33. }

  
  1. package com.example.springbootmongodb.controller;
  2. import com.example.springbootmongodb.domian.Users;
  3. import org.springframework.beans.factory. annotation.Autowired;
  4. import org.springframework. data.mongodb.core.MongoTemplate;
  5. import org.springframework. data.mongodb.core.query.Criteria;
  6. import org.springframework. data.mongodb.core.query.Query;
  7. import org.springframework. data.mongodb.core.query.Update;
  8. import org.springframework.web.bind. annotation.*;
  9. import java.util.List;
  10. @RequestMapping("/mongo")
  11. @RestController
  12. public class UserController {
  13. @Autowired
  14. private MongoTemplate mongoTemplate;
  15. @RequestMapping(value = "/hello",method = RequestMethod.GET)
  16. public String hello(){
  17. return "hello";
  18. }
  19. /**
  20. * 新增
  21. */
  22. @ResponseBody
  23. @RequestMapping(value = "/insert", method = RequestMethod.GET)
  24. public ResultObject insert(){
  25. Users user = new Users();
  26. user.setUserId( "1002");
  27. user.setName( "eric");
  28. user.setAge( 18);
  29. mongoTemplate.insert(user);
  30. return new ResultObject( 200);
  31. }
  32. /**删除
  33. *
  34. * @return
  35. */
  36. @ResponseBody
  37. @RequestMapping("/delete")
  38. public ResultObject delete(){
  39. Query query = Query.query(Criteria. where( "userId"). is( "1001"));
  40. mongoTemplate.remove(query, "mydb");
  41. return new ResultObject( 200);
  42. }
  43. /**
  44. * 删除
  45. * @return
  46. */
  47. @ResponseBody
  48. @RequestMapping("/update")
  49. public ResultObject update(){
  50. Query query = Query.query(Criteria. where( "userId"). is( "1002"));
  51. Update update = new Update();
  52. update. set( "name", "name1");
  53. update. set( "age", 18);
  54. mongoTemplate.updateFirst(query,update, "mydb");
  55. return new ResultObject( 200);
  56. }
  57. /**
  58. * 查询
  59. * @return
  60. */
  61. @ResponseBody
  62. @RequestMapping("/query")
  63. public ResultObject query(){
  64. Query query = Query.query(Criteria. where( "userId"). is( "1001"));
  65. List<Users> list = mongoTemplate.find(query,Users. class);
  66. return new ResultObject( 200,list);
  67. }
  68. }

                                      关注公号“Java高级架构师”,回复“面试题” 获取:高清3585页大厂面试真题


转载:https://blog.csdn.net/qq_17010193/article/details/114861203
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场