Spring Boot(十四):NoSql数据库redis 发表于 2018-09-02 | 分类于 SpringBoot | | 阅读次数: 介绍pom文件引入: 1234<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency> 添加配置: 12345678910#redis不配置会读取默认配置spring.redis.host=localhostspring.redis.port=6379#spring.redis.password=123456#spring.redis.database=0#spring.redis.pool.max-active=8#spring.redis.pool.max-idle=8#spring.redis.pool.max-wait=-1#spring.redis.pool.min-idle=0#spring.redis.timeout=0 实现类: 12345678910111213141516171819202122@Componentpublic class RedisComponent { @Autowired private StringRedisTemplate stringRedisTemplate; public void set(String key, String value) { ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue(); if (!this.stringRedisTemplate.hasKey(key)) { ops.set(key, value); System.out.println("set key success"); } else { // 存在则打印之前的 value 值 System.out.println("this key = " + ops.get(key)); } } public String get(String key) { return this.stringRedisTemplate.opsForValue().get(key); } public void del(String key) { this.stringRedisTemplate.delete(key); }} 测试类: 1234567891011121314@Autowiredprivate RedisComponent redisComponent;@Testpublic void set() { redisComponent.set("dodd", "hello world");}@Testpublic void get() { System.out.println(redisComponent.get("dodd"));}@Testpublic void del() { redisComponent.del("dodd");}