首页 > 其他 > 详细

redis入门案例

时间:2019-09-25 23:20:14      阅读:107      评论:0      收藏:0      [点我收藏+]

1.引入jar包

<!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>

        <!--添加spring-datajar包  -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>

2.String类型测试

//测试字符串 IP:6379
    @Test
    public void testString(){
        Jedis jedis = new Jedis("192.168.126.166",6379);
        jedis.set("aa","1807班");
        System.out.println(jedis.get("aa"));
    }

 

3.hash类型数据测试

//测试hash数据存储
    @Test
    public void testHash(){
        Jedis jedis = new Jedis("192.168.126.166",6379);
        jedis.hset("dog", "id", "100");
        jedis.hset("dog", "name", "中华田园犬");
        jedis.hset("dog", "age", "88");
        System.out.println(jedis.hget("dog", "name"));
        System.out.println(jedis.hgetAll("dog"));
        
    }

 

4.List类型测试

//测试List集合
    @Test
    public void testList(){
        Jedis jedis = new Jedis("192.168.126.166",6379);
        jedis.lpush("idList", "1","2","3");
        System.out.println(jedis.lpop("idList"));
    }

 

5.事务控制

//redis中事务控制
    @Test
    public void testTx(){
        Jedis jedis = new Jedis("192.168.126.166",6379);
        Transaction transaction = jedis.multi();    //开启事务.
        transaction.set("aa", "aaa");
        transaction.set("bb", "bbb");
        transaction.exec();    //事务提交
        System.out.println("执行成功!!!");
        //transaction.discard();    //事务回滚
    }

 

redis入门案例

原文:https://www.cnblogs.com/gxlaqj/p/11588201.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!