|
| 1 | +``` |
| 2 | +1.引入web依赖和mongodb依赖: |
| 3 | + <dependency> |
| 4 | + <groupId>org.springframework.boot</groupId> |
| 5 | + <artifactId>spring-boot-starter-data-mongodb</artifactId> |
| 6 | + </dependency> |
| 7 | + <dependency> |
| 8 | + <groupId>org.springframework.boot</groupId> |
| 9 | + <artifactId>spring-boot-starter-web</artifactId> |
| 10 | + </dependency> |
| 11 | +2.Mongo Shell或者Mongo Compass工具创建数据库testdb,并新增user文档: |
| 12 | + (1)使用Mongo Shell: |
| 13 | + 1)创建数据库testdb: use testdb |
| 14 | + 2)新增user文档: db.createCollection(user) |
| 15 | + (2)使用Mongo Compass工具创建数据库: 图形化操作 |
| 16 | +3.配置文件application.yml里配置Mongo DB: |
| 17 | + spring: |
| 18 | + data: |
| 19 | + mongodb: |
| 20 | + host: localhost |
| 21 | + # MongoDB的默认端口为27017 |
| 22 | + port: 27017 |
| 23 | + database: testdb |
| 24 | +4.创建User实体类: |
| 25 | + // 声明文档对象,名称为user |
| 26 | + @Document(collection = "user") |
| 27 | + public class User { |
| 28 | + //@Id标注主键字段 |
| 29 | + @Id |
| 30 | + private String id; |
| 31 | + private String name; |
| 32 | + private Integer age; |
| 33 | + private String description; |
| 34 | + // get set 略 |
| 35 | + } |
| 36 | + (String类型的主键值插入MongoDB时会自动生成;若对象中某属性为非表字段, |
| 37 | + 可用注解@Transient排除) |
| 38 | +5.编写Mapper接口: |
| 39 | + @Repository |
| 40 | + public interface UserMapper extends MongoRepository<User, String> { |
| 41 | + //通过年龄段,用户名,描述(模糊查询)(自定义方法) |
| 42 | + List<User> findByAgeBetweenAndNameEqualsAndDescriptionIsLike(Integer from, |
| 43 | + Integer to, String name, String description); |
| 44 | + } |
| 45 | + (MongoRepository提供了一些增删改查的方法,通过继承可直接使用) |
| 46 | + (自定义方法,在输入findBy后,IDEA会根据实体对象的属性和SQL的各种关键字自动组合提示) |
| 47 | +6.编写Service接口: |
| 48 | + public interface UserService { |
| 49 | + // 查询文档内所有数据 |
| 50 | + List<User> selectAll(); |
| 51 | + // 通过id查询文档内的数据 |
| 52 | + Optional<User> selectById(String id); |
| 53 | + // 向文档内插入一条记录 |
| 54 | + User create(User user); |
| 55 | + // 通过id更新文档内容 |
| 56 | + void updateById(String id,User user); |
| 57 | + // 通过id删除文档内容 |
| 58 | + void deleteById(String id); |
| 59 | + // 通过条件进行模糊查询,并将结果进行分页 |
| 60 | + Page<User> selectByCondition(int size, int page, User user); |
| 61 | + } |
| 62 | +7.编写Service实现类: |
| 63 | + @Service |
| 64 | + public class UserServiceImpl implements UserService { |
| 65 | + // 排序和分页需要使用MongoTemplate对象来完成 |
| 66 | + @Autowired |
| 67 | + private MongoTemplate template; |
| 68 | + @Autowired |
| 69 | + private UserMapper mapper; |
| 70 | + @Override |
| 71 | + public List<User> selectAll() { |
| 72 | + return this.mapper.findAll(); |
| 73 | + } |
| 74 | + @Override |
| 75 | + public Optional<User> selectById(String id) { |
| 76 | + return this.mapper.findById(id); |
| 77 | + } |
| 78 | + @Override |
| 79 | + public User create(User user) { |
| 80 | + user.setId(null); |
| 81 | + return this.mapper.save(user); |
| 82 | + } |
| 83 | + @Override |
| 84 | + public void updateById(String id, User user) { |
| 85 | + this.mapper.findById(id) |
| 86 | + .ifPresent( |
| 87 | + u -> { |
| 88 | + u.setName(user.getName()); |
| 89 | + u.setAge(user.getAge()); |
| 90 | + u.setDescription(user.getDescription()); |
| 91 | + this.mapper.save(u); |
| 92 | + } |
| 93 | + ); |
| 94 | + } |
| 95 | + @Override |
| 96 | + public void deleteById(String id) { |
| 97 | + this.mapper.findById(id) |
| 98 | + .ifPresent( user -> this.mapper.deleteById(id)); |
| 99 | + } |
| 100 | + // size表示每页显示的条数,page表示当前页码数(0表示第一页) |
| 101 | + // 通过name和description来模糊查询用户信息再分页,并且查询结果使用age字段降序排序 |
| 102 | + @Override |
| 103 | + public Page<User> selectByCondition(int size, int page, User user) { |
| 104 | + Query query = new Query(); |
| 105 | + Criteria criteria = new Criteria(); |
| 106 | + if(!StringUtils.isEmpty(user.getName())){ |
| 107 | + criteria.and("name").is(user.getName()); |
| 108 | + } |
| 109 | + if(!StringUtils.isEmpty(user.getDescription())){ |
| 110 | + criteria.and("description").regex(user.getDescription()); |
| 111 | + } |
| 112 | + query.addCriteria(criteria); |
| 113 | + Sort sort = new Sort(Sort.Direction.DESC,"age"); |
| 114 | + PageRequest pageable = PageRequest.of(page, size, sort); |
| 115 | + List<User> users = template.find(query.with(pageable), User.class); |
| 116 | + return PageableExecutionUtils.getPage(users,pageable, |
| 117 | + ()-> template.count(query,User.class)); |
| 118 | + } |
| 119 | + } |
| 120 | +8.编写controller: (RESTful风格) |
| 121 | + @RestController |
| 122 | + public class UserController { |
| 123 | + @Autowired |
| 124 | + private UserService service; |
| 125 | + @GetMapping("/user/") |
| 126 | + public List<User> getUsers(){ |
| 127 | + return this.service.selectAll(); |
| 128 | + } |
| 129 | + @GetMapping("/user/{id}") |
| 130 | + public User getUser(@PathVariable String id){ |
| 131 | + return this.service.selectById(id).orElse(null); |
| 132 | + } |
| 133 | + @PostMapping("/user/") |
| 134 | + public User createUser(User user){ |
| 135 | + return this.service.create(user); |
| 136 | + } |
| 137 | + @DeleteMapping("/user/{id}") |
| 138 | + public void deleteUser(@PathVariable String id){ |
| 139 | + this.service.deleteById(id); |
| 140 | + } |
| 141 | + @PutMapping("/user/{id}") |
| 142 | + public void updateUser(@PathVariable String id,User user){ |
| 143 | + this.service.updateById(id,user); |
| 144 | + } |
| 145 | + @GetMapping("/user/condition") |
| 146 | + public Page<User> getUserByCondition(int size,int page,User user){ |
| 147 | + return this.service.selectByCondition(size, page, user); |
| 148 | + } |
| 149 | + } |
| 150 | +``` |
0 commit comments