用户工具

站点工具


didi:mongodb
  • 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
  • 配置
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
  • 实体类
public class User {

    @Id
    private Long id;

    private String username;
    private Integer age;

    public User(Long id, String username, Integer age) {
        this.id = id;
        this.username = username;
        this.age = age;
    }

    // 省略getter和setter

}
  • 扩展接口
public interface UserRepository extends MongoRepository<User, Long> {

    User findByUsername(String username);

}
didi/mongodb.txt · 最后更改: 2018/07/30 18:19 由 guest