SpringBoot系列—Redis使用

本文介绍了SpringBoot如何通过Spring Data Redis与Redis进行集成,详细讲解了自动配置的JedisConnectionFactory、redisTemplate及stringRedisTemplate的使用。在配置文件中,通过'spring.redis'前缀设置Redis数据库。在实际操作中,通过docker部署Redis,并遇到序列化问题,导致数据在客户端显示为乱码。为了解决这个问题,需要自定义redistemplate并指定Serializer,例如使用Jackson进行序列化。但在实现过程中,由于User类缺少默认构造函数,导致JsonMappingException。解决方法是让User类实现序列化接口或添加默认构造函数。
摘要由CSDN通过智能技术生成

SpringBoot对redis的支持是通过Spring Data Redis 来时闲的,使用之前引入jar包:

compile('org.springframework.boot:spring-boot-starter-data-redis')

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration为我们默认配置了JedisConnectionFactory、redisTemplate以及stringRedisTemplate,让我们直接可以使用redis作为数据存储。

org.springframework.boot.autoconfigure.data.redis.RedisProperties给我们展示了可以使用以“spring.redis”为前缀的属性在application.yml中配置redis。

例如,默认数据库为db0,在配置文件中将其设置为db1

spring:
  redis:
    database: 1

1.在docker下载redis镜像,并运行容器

2.新建对象User

package com.example.demo.bean;

import java.io.Serializable;

/**
 * SpringBootDemo1
 * Created by xian.juanjuan on 2017-6-19 14:15.
 */
public class User implements Serializable{

    private static final long serialVersionUID = 934073895746700367L;
    private String id;
    private String name;
    private Integer age;

    public User(Integer age, String id, String name) {
        super();
        this.age = age;
        this.id = id;
        this.name = name;
    }

    get,set方法省略....
}
3.数据访问

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架,而Redis是一个开源的内存数据存储系统。结合使用Spring Boot和Redis可以实现高效的数据缓存和持久化。 在Spring Boot中使用Redis,首先需要在项目的pom.xml文件中添加Redis的依赖。然后,在application.properties或application.yml文件中配置Redis的连接信息,包括主机名、端口号、密码等。 接下来,可以通过使用Spring Data Redis来简化对Redis的操作。Spring Data Redis提供了一系列的注解和模板类,可以方便地进行数据的读取、写入和删除等操作。 以下是一个简单的示例,演示了如何在Spring Boot中使用Redis: 1. 添加依赖: 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息: 在application.properties或application.yml文件中添加以下配置: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis操作类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisUtil { @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } public void delete(String key) { redisTemplate.delete(key); } } ``` 4. 使用Redis操作类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private RedisUtil redisUtil; @GetMapping("/user/{id}") public User getUser(@PathVariable String id) { // 先从缓存中获取数据 User user = (User) redisUtil.get("user:" + id); if (user == null) { // 如果缓存中不存在,则从数据库中获取数据 user = userService.getUserById(id); // 将数据存入缓存 redisUtil.set("user:" + id, user); } return user; } } ``` 以上示例中,我们创建了一个RedisUtil类来封装对Redis的操作,然后在UserController中使用RedisUtil来实现对用户数据的缓存。当请求用户数据时,先从缓存中获取,如果缓存中不存在,则从数据库中获取,并将数据存入缓存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值