一、依赖
-
org.springframework.boot -
spring-boot-starter-data-mongodb -
二、配置
- spring:
- data:
- mongodb:
- host: 127.0.0.1
- port: 27017
- database: test
三、实体类
- @Data
- @Document("user")
- @NoArgsConstructor
- @AllArgsConstructor
- public class User {
-
- @Indexed
- @Field("id")
- private Integer id;
-
- @Field("name")
- private String name;
-
- @Field("age")
- private Integer age;
-
- }
四、持久层
- public interface UserRepository extends MongoRepository
{ -
- /**
- * 通过id查询
- * @param id
- * @return
- */
- Optional
findById(Integer id); - }