一、Redis Jedis

1 导入pom节点

   <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

2 连接 Redis

private Jedis jedis;

    @Before
    public void before(){
        //连接jedis
        jedis=new Jedis("192.168.201.22");
        jedis.ping();
        System.out.println("连接成功");

    }

3 Jedis操作String类型数据

   //jedis操作String类
    @Test
    public void  operationString(){
        jedis.select(1);
        //设置一个String类型的key-value
        jedis.set("setkey01","setvalue01");
        //设置key有效时间
        jedis.expire("k1",10);
        String setkey01 = jedis.get("setkey01");
        System.out.println(setkey01);
        //修改 数据
        jedis.set("setkey01","setvalue02");
        //删除数据
        jedis.del("setkey01");
        //设置多个值
        jedis.mset("strkey01","strvalue01","strkey02","strvalue02","strkey03","strvalue03","strkey04","1");
        //+1操作
        jedis.incr("strkey04");
        //-1操作
        jedis.decr("strkey04");
        //拼接
        jedis.append("strkey04","append");
    }

4 jedis操作List集合

//操作List集合
    @Test
    public void  operationlist(){
        jedis.select(1);
        //设置李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, "赵六");

    }

5 jedis操作无序set集合

//操作无序set集合
    @Test
    public void operationset(){
        jedis.select(1);
        //设置set集合
        jedis.sadd("setkey01","张三","李四","王五");
        //读取
        Set<String>  setkey01 =jedis.smembers("setkey01");
        for (String item:setkey01){
            System.out.println(item);
        }
    }

6 jedis操作有序set集合 

//操作有序set集合
    @Test
    public void operationsortedset(){
        jedis.select(1);
        //设置SortedSet集合
        jedis.zadd("setkey02",1,"北京");
        jedis.zadd("setkey02",2,"上海");
        //获取SortedSet
        Set<String>setkey02 =jedis.zrange("setkey02",0,-1);
        for (String item:setkey02){
            System.out.println(item);
        }
    }

7 jedis操作map集合 

//操作map集合
    @Test
    public void operationmap(){
        jedis.select(1);
        //设置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代码

1 导入pom节点 

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
  </parent>
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

2 创建配置文件application.yml

3 创建SpringBootRedis类 

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

    public void set(String key,Object value){
        if (value instanceof String){
            stringRedisTemplate.opsForValue().set(key,(String)value,200L, TimeUnit.SECONDS);
        }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层RedisController

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

    //操作String类型数据
    @RequestMapping("/setString")
    public void setString(){
        springBootRedis.set("strkey","strvalue");
    }

    //操作list集合数据 
    @RequestMapping("/setList")
    public void setList(){
        List<String > strList=new ArrayList<>();
        strList.add("张三");
        strList.add("李四");
        strList.add("王五");
        springBootRedis.set("listkey",strList);
    }

    //操作set集合数据
    @RequestMapping("/setSet")
    public void setSet(){
        Set<String> set =new HashSet<>();
        set.add("北京");
        set.add("上海");
        springBootRedis.set("setkey",set);
    }

    //操作map集合数据
    @RequestMapping("/setMap")
    public void setMap(){
        Map map=new HashMap();
        map.put("name","张三");
        map.put("age","18");
        springBootRedis.set("setmap",map);
    }
}
12-26 22:46