SpringBoot07-NoSql

x33g5p2x  于2021-03-14 发布在 其他  
字(1.3k)|赞(0)|评价(0)|浏览(410)

Spring Boot 集成Redis

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

基础配置

#redis
spring.redis.host=localhost
spring.redis.port=6379

#spring.redis.password=123456
#spring.redis.database=0
#spring.redis.pool.max-active=8
#spring.redis.pool.max-idle=8
#spring.redis.pool.max-wait=-1
#spring.redis.pool.min-idle=0
#spring.redis.timeout=0

注入

@Autowired
private StringRedisTemplate stringRedisTemplate;

Spring Boot 集成MongoDB

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

基础配置

# MONGODB
spring.data.mongodb.uri=mongodb://localhost/test
spring.data.mongodb.port=27017

#spring.data.mongodb.authentication-database=
#spring.data.mongodb.database=test
#spring.data.mongodb.field-naming-strategy=
#spring.data.mongodb.grid-fs-database=
#spring.data.mongodb.host=localhost
#spring.data.mongodb.password=
#spring.data.mongodb.repositories.enabled=true
#spring.data.mongodb.username=

注入

@Autowired
private MongoTemplate mongoTemplate;

Spring Data 的MongoDB操作

与前面的JPA一样,Spring Data也实现了一个MongoDB的资源库,可以根据方法名操作MongoDB数据库,只需要定义接口继承`MongoRepository

public interface UserMongoDBRepository extends MongoRepository<User,String>
...

```

相关文章