个人技术分享

Spring Boot项目中集成Redis,可以使用Spring Data Redis来简化操作。首先需要在pom.xml文件中添加Redis和Spring Data Redis的依赖:

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

然后在application.xml文件中配置Redis的连接信息:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://11X.X.113.45:3306/xuuexi?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

  redis:
    host: 11X.X.113.45
    prot: 6379
    password: 123456

写Controller编译测试

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;


    private static final Logger logger = LoggerFactory.getLogger(RedisController.class);
    @GetMapping("/redis")
    public void loredisg(){
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        //查询key
        String value1 = opsForValue.get("test-key");
        logger.info("写入前值为:{}",value1);
        opsForValue.set("test-key","zhong终于实现了哈喽word");
        String value2 = opsForValue.get("test-key");
        logger.info("写入后值为:{}",value2);
    }
}