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


    6.1、引入redis的maven配置

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-data-redis</artifactId>
    4. </dependency>

    6.2、application.properties中配置redis信息

    1. spring.redis.host=192.168.200.129
    2. spring.redis.port=6379
    3. #spring.redis.password=root
    4. spring.redis.timeout=60000
    5. spring.redis.database=0

    6.3、使用RedisTemplate工具类操作redis

    springboot中使用RedisTemplate来操作redis,需要在我们的bean中注入这个对象,代码如下:

    1. @Autowired
    2. private RedisTemplate<String, String> redisTemplate;
    3. // 用下面5个对象来操作对应的类型
    4. this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法
    5. this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法
    6. this.redisTemplate.opsForSet(); //提供了操作set的所有方法
    7. this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法
    8. this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法

    6.4、RedisTemplate示例代码

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.data.redis.core.RedisTemplate;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.HashMap;
    6. import java.util.List;
    7. import java.util.Map;
    8. import java.util.Set;
    9. @RestController
    10. @RequestMapping("/redis")
    11. public class RedisController {
    12. @Autowired
    13. private RedisTemplate<String, String> redisTemplate;
    14. @RequestMapping("/stringTest")
    15. public String stringTest() {
    16. this.redisTemplate.delete("name");
    17. this.redisTemplate.opsForValue().set("name", "路人");
    18. String name = this.redisTemplate.opsForValue().get("name");
    19. return name;
    20. }
    21. @RequestMapping("/listTest")
    22. public List<String> listTest() {
    23. this.redisTemplate.delete("names");
    24. this.redisTemplate.opsForList().rightPushAll("names", "刘德华", "张学友", "郭富城", "黎明");
    25. List<String> courses = this.redisTemplate.opsForList().range("names", 0, -1);
    26. return courses;
    27. }
    28. @RequestMapping("setTest")
    29. public Set<String> setTest() {
    30. this.redisTemplate.delete("courses");
    31. this.redisTemplate.opsForSet().add("courses", "java", "spring", "springboot");
    32. Set<String> courses = this.redisTemplate.opsForSet().members("courses");
    33. return courses;
    34. }
    35. @RequestMapping("hashTest")
    36. public Map<Object, Object> hashTest() {
    37. this.redisTemplate.delete("userMap");
    38. Map<String, String> map = new HashMap<>();
    39. map.put("name", "路人");
    40. map.put("age", "30");
    41. this.redisTemplate.opsForHash().putAll("userMap", map);
    42. Map<Object, Object> userMap = this.redisTemplate.opsForHash().entries("userMap");
    43. return userMap;
    44. }
    45. @RequestMapping("zsetTest")
    46. public Set<String> zsetTest() {
    47. this.redisTemplate.delete("languages");
    48. this.redisTemplate.opsForZSet().add("languages", "java", 100d);
    49. this.redisTemplate.opsForZSet().add("languages", "c", 95d);
    50. this.redisTemplate.opsForZSet().add("languages", "php", 70);
    51. Set<String> languages = this.redisTemplate.opsForZSet().range("languages", 0, -1);
    52. return languages;
    53. }
    54. }

    来源:http://www.itsoku.com/course/15/253

  • 相关阅读:
    如何设定测试目标
    转载:Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)
    Jenkins启动时报错:java.net.BindException: Address already in use: bind 解决方法
    [转载]Robotium API 翻译(三)——判断测试结果的方法assert、is、search
    什么样的项目适合开展自动化测试
    Python基础11- 函数之自定义函数
    Python基础10- 函数之内部函数与强制转换
    Android获取APK包名的几种方法
    Python基础9- 字典
    回归测试策略
  • 原文地址:https://www.cnblogs.com/konglxblog/p/16198229.html
Copyright © 2020-2023  润新知