Springboot集成MongoDB實現CRUD

特別提示:本人博客部分有參考網絡其餘博客,但均是本人親手編寫過並驗證經過。如發現博客有錯誤,請及時提出以避免誤導其餘人,謝謝!歡迎轉載,但記得標明文章出處: http://www.cnblogs.com/mao2080/

一、添加依賴

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>

二、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

三、操做過程

  注意使用postman來模擬POST請求。java

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

  二、刪除URL:http://localhost:8080/mongodbController/delete?userId=015spring

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

  四、查詢URL:http://localhost:8080/mongodbController/query網絡

四、代碼下載

  mongodb-demo.zipapp

五、參考網站

  https://blog.csdn.net/mazhen1991/article/details/78161784spring-boot

相關文章
相關標籤/搜索