一、Redis的基本数据类型(jedis操作类型数据

  1、导入依赖(导入Redis依赖,让项目支持使用Redis

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

<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>

  2、创建一个普通Java类,导入Jedis并完成基本配置

    

  3、类型演示

    (1)jedis操作String类型数据

   /**
     * jedis操作String类型数据
     */
    @Test
    public void operationString(){
        //切换到数据库
        jedis.select(1);
        //设置一个String类型的key-value(添加数据)
        jedis.set("strKey01","strValue01");
        //设置key的有效时间
        //jedis.expire("strKey01",20);
        //获取String数据
        String value = jedis.get("strKey01");
        System.out.println("对应存放的value值为:"+value);
        //修改数据
        jedis.set("strKey01","strValue02");
        //删除数据
        jedis.del("strKey01");
        //设置多个值
        jedis.mset("strKey01","strValue01","strKey02","strValue01","strKey03","strValue03","strKey04","1");
        //+1操作
        jedis.incr("strKey04");
        //-1操作
        jedis.decr("strKey04");
        //拼接
        jedis.append("strKey04","append");
    }

      执行效果如下

    

     

    (2)jedis操作List类型数据

   /**
     * jedis操作List类型数据
     */
    @Test
    public void operationList(){
        //清空集合值
        jedis.del("listkey01","listkey02");
        //设置List集合值,左侧插入
        jedis.lpush("listkey01","张三","李四","王五");
        //设置List集合值,右侧插入
        jedis.rpush("listkey02","北京","上海","广州");
        //过去List集合值
        List<String> listkey01 = jedis.lrange("listkey01", 0, -1);
        for (String item:listkey01){
            System.out.println(item);
        }
        //更改List集合数据
        jedis.lset("listkey01",0,"赵六");
    }

      执行结果如下

    

     

     (3)jedis操作Set类型数据

    /**
     * jedis操作Set类型数据
     */
    @Test
    public void operationSet(){
        //切换到数据库
        jedis.select(2);
        //设置Set集合
        jedis.sadd("setkey01","张三","李四","王五");
        //获取
        Set<String> setkey01 = jedis.smembers("setkey01");
        for (String item:setkey01){
            System.out.println(item);
        }
    }

      执行结果如下

    

     

     (4)jedis操作SortedSet(有序set集合)类型数据

    /**
     * jedis操作SortedSet(有序set集合)类型数据
     */
    @Test
    public void operationSortedSet(){
        //切换到数据库
        jedis.select(2);
        //设置SortedSet集合
        jedis.zadd("setkey02",1,"北京");
        jedis.zadd("setkey02",1,"上海");
        jedis.zadd("setkey02",1,"广州");
        //获取SortedSet
        Set<String> setkey02 = jedis.zrange("setkey02", 0, -1);
        for (String item:setkey02){
            System.out.println(item);
        }
    }

      执行结果如下

    

    

    (5)jedis操作Map类型数据

     /**
     * jedis操作Map类型数据
     */
    @Test
    public void operationMap(){
        //切换到数据库
        jedis.select(3);
        //设置Map
        Map<String,String> map=new HashMap<>();
        map.put("name","张三");
        map.put("age","18");
        jedis.hmset("mapkey01",map);

        //获取值
        List<String> value = jedis.hmget("mapkey01", "name", "age");
        for (String item:value){
            System.out.println(item);
        }

      执行结果如下

    

    

 二、只用Java操作Redis

  1、导入依赖

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

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

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

  2、配置Application.yml文件

    

   3、创建SpringBootRedis类

package com.jedis.service;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class SpringBootRedis {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void set(String key,Object value){
        if (value instanceof String){
            //200L, TimeUnit.SECONDS:用来设置时间,TimeUnit.SECONDS相当于设置单位
            //stringRedisTemplate.opsForValue().set(key,(String)value,200L, TimeUnit.SECONDS);
            stringRedisTemplate.opsForValue().set(key,(String)value);
        }else if(value instanceof List){
            List<String> list=(List)value;
            for (String item:list){
                stringRedisTemplate.opsForList().leftPush(key,item);
            }
        }else if (value instanceof Set){
            String[] objects = (String[]) ((Set) value).toArray(new String[((Set) value).size()]);
            stringRedisTemplate.opsForSet().add(key,objects);
        }else if (value instanceof Map){
            stringRedisTemplate.opsForHash().putAll(key,(Map<?,?>) value);
        }
    }
}

  4、创建controller类

package com.jedis.controller;

import com.jedis.service.SpringBootRedis;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class RedisController {
    @Resource
    private SpringBootRedis springBootRedis;

    @RequestMapping ("/setString")
    public void setString(HttpServletResponse response){
        springBootRedis.set("strkey","strvalue");
    }

    @RequestMapping("/setList")
    public void setList(HttpServletResponse response){
        List<String> stringList=new ArrayList<>();
        stringList.add("张三");
        stringList.add("李四");
        stringList.add("王五");
        springBootRedis.set("listkey",stringList);
    }

    @RequestMapping("/setSet")
    public void setSet(HttpServletResponse response){
        Set<String> set=new HashSet<>();
        set.add("北京");
        set.add("上海");
        springBootRedis.set("setkey",set);
    }

    @RequestMapping("/setMap")
    public void setMap(HttpServletResponse response){
        Map map=new HashMap();
        map.put("name","张三");
        map.put("age","18");
        springBootRedis.set("mapkey",map);
    }
}

  5、创建启动类StartApplication

package com.jedis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartApplication {
    public static void main(String[] args){
        SpringApplication.run(StartApplication.class,args);
    }
}

  6、实现效果如下

     

    (1)http://localhost:8080/setString

      

     (2)http://localhost:8080/setList

      

     (3)http://localhost:8080/setSet

      

    (4)http://localhost:8080/setMap

      

 

 

 

    

posted on 2019-12-30 14:45  婷好  阅读(1405)  评论(0编辑  收藏  举报