SpringBoot集成Redis

    技术2022-07-11  81

    SpringBoot集成Redis

    1、依赖

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>

    2、配置

    spring.redis.database=0 spring.redis.host=192.168.66.130 spring.redis.port=6379 spring.redis.password=123@456 spring.redis.lettuce.pool.max-active= spring.redis.lettuce.pool.max-idle= spring.redis.lettuce.pool.max-wait= spring.redis.lettuce.pool.min-idle= spring.redis.lettuce.shutdown-timeout= spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1ms spring.redis.jedis.pool.min-idle=0

    3、使用

    package org.sang; import java.io.Serializable; @Data public class Book implements Serializable { private Integer id; private String name; private String author; } package org.sang; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; 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 BookController { @Autowired RedisTemplate redisTemplate; @Autowired StringRedisTemplate stringRedisTemplate; @GetMapping("/test1") public void test1() { ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue(); ops1.set("name", "三国演义"); String name = ops1.get("name"); System.out.println(name); ValueOperations ops2 = redisTemplate.opsForValue(); Book b1 = new Book(); b1.setId(1); b1.setName("红楼梦"); b1.setAuthor("曹雪芹"); ops2.set("b1", b1); Book book = (Book) ops2.get("b1"); System.out.println(book); } }
    Processed: 0.011, SQL: 9