在java中操作Redis

创建项目

直接Next
在这里插入图片描述
填写完整之后finish

jedis

导入依赖,一个测试,一个jedis

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.0</version>
</dependency>

测试

package com.tmg.text;


import org.junit.Test;
import redis.clients.jedis.Jedis;

/**
 * 使用jedis操作redis
 */
public class JedisText {
    @Test
    public void testRedis(){
        //获取连接
        Jedis jedis = new Jedis("192.168.93.1",6379);
        //执行操作
        String set = jedis.set("username", "tmg");
        jedis.hset("school","name","hblg");
        System.out.println(set);
        //关闭连接
        jedis.close();
    }

}

结果图:
在这里插入图片描述
在这里插入图片描述

Spring data redis

springboot项目:

在这里插入图片描述

导入依赖

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

配置文件

spring.application.name=springdataredis
spring.redis.host=192.168.93.1
spring.redis.port=6379
spring.redis.database=0
# 连接数量
spring.redis.jedis.pool.max-active=8
# 阻塞等待时间
spring.redis.jedis.pool.max-wait=1ms
# 空闲连接
spring.redis.jedis.pool.max-idle=4
spring.redis.jedis.pool.min-idle=0

测试类

package com.tmg;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringdataredisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void textString() {
        System.out.println(redisTemplate);
        redisTemplate.opsForValue().set("name","tmg5201314");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }

}

配置序列化类

上面的测试运行结果,name有十六进制,就是序列化后的结果,如果想要保存什么,jedis就是什么样子就需要配置序列化

在这里插入图片描述
value不需要,读取的时候会还原回来

package com.tmg.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate();
//        默认的key虚拟化器JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        System.out.println(redisTemplate);
        return redisTemplate;
    }

}

使用StringRedisTemplate 类

就不会出现看不懂的十六进制
在这里插入图片描述

使用方式

只需要注入即可,无需配置类
@Autowired
    private StringRedisTemplate stringRedisTemplate;

@Test
    public void stringRedisTemplateTextString() {
        stringRedisTemplate.opsForValue().set("userName","tmg1314");
        System.out.println(stringRedisTemplate.opsForValue().get("userName"));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值