• Springboot集成MongoDB实现CRUD


    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/

    1、添加依赖

    1        <dependency>
    2            <groupId>org.springframework.boot</groupId>
    3            <artifactId>spring-boot-starter-web</artifactId>
    4        </dependency>
    5        <dependency>
    6            <groupId>org.springframework.boot</groupId>
    7            <artifactId>spring-boot-starter-data-mongodb</artifactId>
    8        </dependency>

    2、Java代码

      a、控制层

     1 package com.mao2080.mongodb;
     2 
     3 import com.mao2080.beans.User;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.data.mongodb.core.MongoTemplate;
     6 import org.springframework.data.mongodb.core.query.Criteria;
     7 import org.springframework.data.mongodb.core.query.Query;
     8 import org.springframework.data.mongodb.core.query.Update;
     9 import org.springframework.web.bind.annotation.*;
    10 
    11 import javax.servlet.http.HttpServletResponse;
    12 import java.util.List;
    13 
    14 @RestController
    15 @RequestMapping("/mongodbController")
    16 public class MongodbController {
    17 
    18     @Autowired
    19     private MongoTemplate mongoTemplate;
    20 
    21     /**表名*/
    22     private static final String collectionName = "user";
    23 
    24     /**
    25      * 描述:新增
    26      * @author maochengyuan
    27      * @created 2018/9/1 20:17
    28      * @param user
    29      * @return ResultObject
    30      */
    31     @RequestMapping(value = "/insert", method = RequestMethod.POST)
    32     @ResponseBody
    33     public ResultObject insert(@ModelAttribute User user) throws Exception {
    34         this.mongoTemplate.insert(user);
    35         return new ResultObject(HttpServletResponse.SC_OK);
    36     }
    37 
    38     /**
    39      * 描述:删除
    40      * @author maochengyuan
    41      * @created 2018/9/1 20:17
    42      * @param userId
    43      * @return ResultObject
    44      */
    45     @RequestMapping("/delete")
    46     @ResponseBody
    47     public ResultObject delete(@RequestParam("userId") String userId) throws Exception {
    48         Query query = Query.query(Criteria.where("userId").is(userId));
    49         this.mongoTemplate.remove(query, collectionName);
    50         return new ResultObject(HttpServletResponse.SC_OK);
    51     }
    52 
    53     /**
    54      * 描述:修改
    55      * @author maochengyuan
    56      * @created 2018/9/1 20:17
    57      * @param user
    58      * @return ResultObject
    59      */
    60     @RequestMapping(value = "/update", method = RequestMethod.POST)
    61     @ResponseBody
    62     public ResultObject update(@ModelAttribute User user) throws Exception {
    63         Query query = Query.query(Criteria.where("userId").is(user.getUserId()));
    64         Update update = new Update();
    65         update.set("age", user.getAge());
    66         update.set("name", user.getName());
    67         update.set("email", user.getEmail());
    68         this.mongoTemplate.updateFirst(query, update, collectionName);
    69         return new ResultObject(HttpServletResponse.SC_OK);
    70     }
    71 
    72     /**
    73      * 描述:查询
    74      * @author maochengyuan
    75      * @created 2018/9/1 20:17
    76      * @param
    77      * @return ResultObject
    78      */
    79     @RequestMapping("/query")
    80     @ResponseBody
    81     public ResultObject query() throws Exception {
    82         Query query = Query.query(Criteria.where("dataStatus").is(1));
    83         List<User> users = this.mongoTemplate.find(query, User.class);
    84         return new ResultObject(HttpServletResponse.SC_OK, users);
    85     }
    86 
    87 }

      b、实体类

     1 package com.mao2080.beans;
     2 
     3 import org.springframework.data.mongodb.core.mapping.Document;
     4 
     5 import java.util.Date;
     6 
     7 @Document(collection="user")
     8 public class User {
     9 
    10     private String userId;
    11 
    12     private String name;
    13 
    14     private String uclass;
    15 
    16     private String email;
    17 
    18     private Date birthday;
    19 
    20     private int age;
    21 
    22     private int dataStatus;
    23 
    24     public User() {
    25     }
    26 
    27     public String getName() {
    28         return name;
    29     }
    30 
    31     public void setName(String name) {
    32         this.name = name;
    33     }
    34 
    35     public String getUserId() {
    36         return userId;
    37     }
    38 
    39     public void setUserId(String userId) {
    40         this.userId = userId;
    41     }
    42 
    43     public String getUclass() {
    44         return uclass;
    45     }
    46 
    47     public void setUclass(String uclass) {
    48         this.uclass = uclass;
    49     }
    50 
    51     public String getEmail() {
    52         return email;
    53     }
    54 
    55     public void setEmail(String email) {
    56         this.email = email;
    57     }
    58 
    59     public Date getBirthday() {
    60         return birthday;
    61     }
    62 
    63     public void setBirthday(Date birthday) {
    64         this.birthday = birthday;
    65     }
    66 
    67     public int getAge() {
    68         return age;
    69     }
    70 
    71     public void setAge(int age) {
    72         this.age = age;
    73     }
    74 
    75     public int getDataStatus() {
    76         return dataStatus;
    77     }
    78 
    79     public void setDataStatus(int dataStatus) {
    80         this.dataStatus = dataStatus;
    81     }
    82 }

    c、配置文件(application.properties)

    1 spring.data.mongodb.host=127.0.0.1
    2 spring.data.mongodb.port=27000
    3 spring.data.mongodb.database=mao2080

    3、操作过程

      注意使用postman来模拟POST请求。

      1、新增URL:http://localhost:8080/mongodbController/insert?userId=015&name=Back&uclass=B&email=b12@sina.com&age=11&dataStatus=1

      2、删除URL:http://localhost:8080/mongodbController/delete?userId=015

      3、修改URL:http://localhost:8080/mongodbController/update?userId=014&name=Back&uclass=B&email=b12@sina.com&age=18&dataStatus=2

      4、查询URL:http://localhost:8080/mongodbController/query

    4、代码下载

      mongodb-demo.zip

    5、参考网站

      https://blog.csdn.net/mazhen1991/article/details/78161784

  • 相关阅读:
    mysql学习笔记——建表需满足的三大范式
    mysql学习笔记——对数据记录查询操作的补充(单表内查询)
    mysql学习笔记——对数据表中记录的操作
    转载----- mysql 五大约束
    mysql笔记------对数据表操作
    c语言中static的用法
    解决Android抽屉被击穿问题
    解决ScrollView与ListView事件冲突
    使用Loader实时查询本地数据库用法
    Android中实现两次点击返回键退出本程序
  • 原文地址:https://www.cnblogs.com/mao2080/p/9571699.html
Copyright © 2020-2023  润新知