上一篇文章中咱們已經簡單搭建了webflux的框架,今天就集成mongodb完成一個用戶管理系統。
https://www.mongodb.com/downl...java
到bin目錄下打開cmd命令窗口 運行:react
mongod.exe --dbpath C:\Tools\mongodb\db
dbpathshi 是設置數據備份目錄,必需要設置,不然啓動不了。web
bin目錄下的 mongo.exe
是mongodb的查詢客戶端,能夠執行查詢操做。一些查詢命令能夠直接去官網看。show dbs
:顯示當前全部文檔庫,至關於數據庫use test
:選擇test庫db.user.find()
:查詢全部user文檔數據db.user.drop()
:刪除全部user文檔spring
pom文件依賴:mongodb
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency>
配置鏈接:數據庫
spring.data.mongodb.host=localhost spring.data.mongodb.database=test spring.data.mongodb.port=27017
package com.mike.dao; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.stereotype.Repository; import com.mike.po.User; /** * The class UserDao.java */ @Repository public interface UserDao extends ReactiveMongoRepository<User, String>{ }
ReactiveMongoRepository 已經幫你實現了增刪該查,若是須要別的方法,須要本身添加實現接口。具體寫法和JPA相似編程
package com.mike.service; 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.stereotype.Service; import com.mike.dao.UserDao; import com.mike.po.User; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** * The class UserService.java */ @Service public class UserService { @Autowired private UserDao userDao; @Autowired private MongoTemplate mongoTemplate; public Mono<User> saveOrUpdateUser(User user){ return userDao.save(user); } public Mono<User> findById(String id){ return userDao.findById(id); } public Flux<User> findAll(){ return userDao.findAll(); } public void deleteById(String id){ // 使用mongoTemplate來作刪除 直接使用提供的刪除方法不行 Query query = Query.query(Criteria.where("id").is(id)); mongoTemplate.remove(query, User.class); //userDao.deleteById(id); 這樣沒法刪除,不知道爲何 } }
package com.mike.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.mike.po.User; import com.mike.service.UserService; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** * The class UserController.java */ @Controller public class UserController { @Autowired private UserService userService; @PostMapping("/user") public String save(User user,final Model model){ Mono<User> u = userService.saveOrUpdateUser(user); model.addAttribute("user", u); return "redirect:/users"; } @GetMapping("/user/find/{id}") @ResponseBody public Mono<User> find(@PathVariable("id") String id){ return userService.findById(id); } @GetMapping("/users") public String findAll(final Model model){ Flux<User> users= userService.findAll(); model.addAttribute("users", users); return "user"; } @GetMapping("/user/delete/{id}") public String delete(@PathVariable("id") String id){ userService.deleteById(id); return "redirect:/users"; } }
package com.mike.po; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Field; /** * The class User.java */ public class User { @Id @Field("_id") private String id; private String name; private String sex; private String job; private String address; private String phone; /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex the sex to set */ public void setSex(String sex) { this.sex = sex; } /** * @return the job */ public String getJob() { return job; } /** * @param job the job to set */ public void setJob(String job) { this.job = job; } /** * @return the address */ public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } /** * @return the phone */ public String getPhone() { return phone; } /** * @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; } }
和正常的關係型數據庫的操做同樣,只不過有些問題想不明白。我上面的刪除舊出現了沒法刪除的問題,最後使用的mongodbTemplate完成的,這是個同步操做。爲何會出現這樣的問題呢?這就是響應式編程的坑,若是你不理解就會出現問題。增刪改查完了,可是沒有頁面展現,寫一篇寫頁面。windows