Java教程

Redis专题之6、第6篇:SpringBoot2整合Redis

本文主要是介绍Redis专题之6、第6篇:SpringBoot2整合Redis,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

6.1、引入redis的maven配置

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

6.2、application.properties中配置redis信息

spring.redis.host=192.168.200.129spring.redis.port=6379#spring.redis.password=rootspring.redis.timeout=60000spring.redis.database=0

6.3、使用RedisTemplate工具类操作redis

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的所有方法

6.4、RedisTemplate示例代码

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

这篇关于Redis专题之6、第6篇:SpringBoot2整合Redis的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!