springboot中使用redis

在springboot中使用redis,首先要在pom.xml中引入redis相关依赖


org.springframework.boot
spring-boot-starter-data-redis

Example.java

@Controller
@SpringBootApplication
public class Example {

@Autowired
private StringRedisTemplate template;

@RequestMapping("/")
    String home(Map<String,Object> map) { 
        map.put("sysinfo", "SpringBoot Demo 之 XXX系统");
        map.put("username","Jack");

        ValueOperations<String, String> ops = this.template.opsForValue();
        String key = "spring.boot.redis.test";
        if (!this.template.hasKey(key)) {
            ops.set(key, "foo");
        }
        System.out.println("Found key " + key + ", value=" + ops.get(key));
        return "index";
    }
    ...

配置文件application.properties

Redis

REDIS (RedisProperties)

Redisu6570u636Eu5E93u7D22u5F15uFF08u9ED8u8BA4u4E3A0uFF09

spring.redis.database=0

Redisu670Du52A1u5668u5730u5740

spring.redis.host=127.0.0.1

Redisu670Du52A1u5668u8FDEu63A5u7AEFu53E3

spring.redis.port=6379

Redisu670Du52A1u5668u8FDEu63A5u5BC6u7801uFF08u9ED8u8BA4u4E3Au7A7AuFF09

spring.redis.password=

u8FDEu63A5u6C60u6700u5927u8FDEu63A5u6570uFF08u4F7Fu7528u8D1Fu503Cu8868u793Au6CA1u6709u9650u5236uFF09

spring.redis.pool.max-active=8

u8FDEu63A5u6C60u6700u5927u963Bu585Eu7B49u5F85u65F6u95F4uFF08u4F7Fu7528u8D1Fu503Cu8868u793Au6CA1u6709u9650u5236uFF09

spring.redis.pool.max-wait=-1

u8FDEu63A5u6C60u4E2Du7684u6700u5927u7A7Au95F2u8FDEu63A5

spring.redis.pool.max-idle=8

u8FDEu63A5u6C60u4E2Du7684u6700u5C0Fu7A7Au95F2u8FDEu63A5

spring.redis.pool.min-idle=0

u8FDEu63A5u8D85u65F6u65F6u95F4uFF08u6BEBu79D2uFF09

spring.redis.timeout=0

至此,完成redis的功能添加。

启动本地redis服务器就能开始测试啦。

0%