4.2 访问 NoSQL 数据库
与关系型数据库一样,Spring Boot 也提供了对 NoSQL 数据库的集成扩展,如对 Redis 和 MongoDB 等数据库的操作。通过默认配置即可使用 RedisTemplate 和 MongoTemplate 等模板类操作非关系型数据库。
4.2.1 访问 Redis
Redis 可以作为缓存、消息中间件或多类型的 key-value 数据库。作为非关系型数据库,Redis 支持多种类型的存储方式,包括 String(字符串)、List(列表)、Hash(哈希)、Set(集合)和 Sorted Set(有序集合)等。
Spring Boot 为 Redis 提供了基本的自动配置,依赖于 spring-boot-starter-data-redis 包。该包提供了自动配置的 RedisConnectionFactory、StringRedisTemplate 和 Redis-Template 实例。如果不配置,则默认连接 localhost:6379 服务器。
StringRedisTemplate 继承自 RedisTemplate,默认采用 String 的序列化策略。如果使用 RedisTemplate,则可以实现自己的序列化方式。
连接 Redis 可以使用 Lettuce 或 Jedis 客户端。Spring Boot 默认使用 Lettuce 客户端。因为 Lettuce 的连接是基于 Netty 的,所以多线程是安全的。
RedisTemplate 提供了以下 5 种数据结构的操作方法:
- opsForValue:操作字符串类型;
- opsForHash:操作哈希类型;
- opsForList:操作列表类型;
- opsForSet:操作集合类型;
- opsForZSet:操作有序集合类型。
下面给出一个集成了 Redis 操作的简单示例。
(1)在 application.yml 配置文件中添加配置,代码如下:
spring:
redis:
host: localhost
port: 6379
password: redistest
timeout: 1000
lettuce:
pool:
max-active: 10 //①
max-wait: 1000 //②
max-idle: 2 //③
min-idle: 0 //④注释①:连接池的最大连接数。
注释②:连接池的最大阻塞等待时间。
注释③:连接池中的最大空闲连接数。
注释④:连接池中的最小空闲连接数。
(2)在 Controller 类中注入 StringRedisTemplate 实例,代码如下:
@RestController
@RequestMapping("/hi")
public class HiController {
//自动注入 StringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/redis/add")
public String redisAdd(int id) {
//Redis set 操作
stringRedisTemplate.opsForValue().set("redis_test_"+id, "redis
test!");
return "1";
}
@GetMapping("/redis/query")
public String redisQuery(int id) {
//Redis get 操作
String val = stringRedisTemplate.opsForValue().get("redis_
test_"+id);
return val;
}
@GetMapping("/redis/delete")
public String redisDelete(int id) {
//Redis delete 操作
stringRedisTemplate.delete("redis_test_"+id);
return "1";
}
}访问接口 http://localhost:8080/hi/redis/add?id=1,可以在 Redis 中添加一个 String 类型的数据;访问接口 http://localhost:8080/hi/redis/query?id=1,可以查询一个 String 类型的数据;访问接口 http://localhost:8080/hi/redis/delete?id=1,可以删除 Redis 数据。
4.2.2 访问 MongoDB
MongoDB 是一个开源的 NoSQL 文档型数据库,使用类 JSON 结构代替传统的基于表结构的关系型数据库。spring-boot-starter-data-mongodb 模块提供了可以操作 MongoDB 的 MongoTemplate 模板类。
Spring Boot 自动配置 org.springframework.data.mongodb.MongoDatabaseFactory 类,默认连接 mongodb://localhost/test 库。同样,也可以自定义一个 MongoClient 来代替 MongoDatabaseFactory 类。
下面给出一个集成 MongoDB 操作的简单示例。
(1)在 application.yml 配置文件中添加 MongoDB 配置,具体代码如下:
spring:
data:
mongodb:
host: localhost
port: 27017
database: user
username: root
password: test1111
authentication-database: admin(2)注入 MongoTemplate 实例,代码如下:
@RestController
@RequestMapping("/hi")
public class HiController {
@Autowired
private MongoTemplate mongoTemplate; //自动注入 MongoTemplate
@GetMapping("/mongo/add")
public Document mongoAdd(String id) {
BasicDBObject db = new BasicDBObject();
db.put("_id", new ObjectId(id));
//插入操作
mongoTemplate.insert(db, "pages");
MongoCollection<Document> collection = mongoTemplate.
getCollection("user");
Document document = collection.find(db).first();
return document;
}
@GetMapping("/mongo/query")
public Document mongoQuery(String id) {
BasicDBObject db = new BasicDBObject();
db.put("_id", new ObjectId(id));
MongoCollection<Document> collection = mongoTemplate.
getCollection("user");
//查询操作
Document document = collection.find(db).first();
return document;
}
}访问接口 http://localhost:8080/hi/mongo/query?id=5a717aa60837d974f4b4a5,即可查询相关的数据。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论