springboot连接redis

导入reids的依赖

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

设置application.yml配置信息

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    generate-ddl: true
  redis:  
    host: 192.168.222.123
    port: 6379
    password: offcn

编写测试类代码

@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloApplication.class)
public class SbootTest {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void getRedis(){
        redisTemplate.opsForValue().set("java","我爱java");
        String str =(String) redisTemplate.opsForValue().get("java");
        System.out.println(str);
        MUser mUser=new MUser();
        mUser.setId(1);
        mUser.setName("aa");
        redisTemplate.opsForValue().set("user",mUser);
        MUser user =(MUser) redisTemplate.opsForValue().get("user");
        System.out.println(user);
    }
}
原文地址:https://www.cnblogs.com/proyuan/p/11803628.html