上一篇文章中咱们已经简单搭建了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