<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
spring.redis.host=192.168.200.129spring.redis.port=6379#spring.redis.password=rootspring.redis.timeout=60000spring.redis.database=0
springboot中使用RedisTemplate来操作redis,需要在我们的bean中注入这个对象,代码如下:
@Autowiredprivate RedisTemplate<String, String> redisTemplate;// 用下面5个对象来操作对应的类型this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法this.redisTemplate.opsForSet(); //提供了操作set的所有方法this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;@RestController@RequestMapping("/redis")public class RedisController { @Autowired private RedisTemplate<String, String> redisTemplate; @RequestMapping("/stringTest") public String stringTest() { this.redisTemplate.delete("name"); this.redisTemplate.opsForValue().set("name", "路人"); String name = this.redisTemplate.opsForValue().get("name"); return name; } @RequestMapping("/listTest") public List<String> listTest() { this.redisTemplate.delete("names"); this.redisTemplate.opsForList().rightPushAll("names", "刘德华", "张学友", "郭富城", "黎明"); List<String> courses = this.redisTemplate.opsForList().range("names", 0, -1); return courses; } @RequestMapping("setTest") public Set<String> setTest() { this.redisTemplate.delete("courses"); this.redisTemplate.opsForSet().add("courses", "java", "spring", "springboot"); Set<String> courses = this.redisTemplate.opsForSet().members("courses"); return courses; } @RequestMapping("hashTest") public Map<Object, Object> hashTest() { this.redisTemplate.delete("userMap"); Map<String, String> map = new HashMap<>(); map.put("name", "路人"); map.put("age", "30"); this.redisTemplate.opsForHash().putAll("userMap", map); Map<Object, Object> userMap = this.redisTemplate.opsForHash().entries("userMap"); return userMap; } @RequestMapping("zsetTest") public Set<String> zsetTest() { this.redisTemplate.delete("languages"); this.redisTemplate.opsForZSet().add("languages", "java", 100d); this.redisTemplate.opsForZSet().add("languages", "c", 95d); this.redisTemplate.opsForZSet().add("languages", "php", 70); Set<String> languages = this.redisTemplate.opsForZSet().range("languages", 0, -1); return languages; }}
来源:http://www.itsoku.com/course/15/253