Spring 使用RedisTemplate操作Redis

Spring 使用RedisTemplate操作Redis

 

 

首先添加依赖:

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

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

 

创建:SpringConfig 

package the_mass.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration  //配置
@ComponentScan("the_mass.redis")  //扫描那个包
public class SpringConfig {

    @Bean
    RedisConnectionFactory redisConnectionFactory(){ //获取连接工厂
        return new JedisConnectionFactory();          //返回
}

    @Bean
    RedisTemplate redisTemplate(){      //模板
        return new StringRedisTemplate(redisConnectionFactory());  //使用连接工厂返回
    }

}
创建:RedisService
package the_mass.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class RedisService {

    @Autowired
    RedisConnectionFactory factory; //通过Redis连接的线程安全工厂

    @Autowired
    RedisOperations redisOperations; //通过公共接口RedisOperations


    public void testRedis() {
        RedisConnection connection = factory.getConnection();
        byte[] bytes = connection.get("hello".getBytes());
        System.out.println(new String(bytes, StandardCharsets.UTF_8));
    }

    public void testRedisTemplate() {
        Object hello = redisOperations.opsForValue().get("hello");
        System.out.println(hello);
    }

}

 

创建:JedisDemo

package the_mass.redis;

import redis.clients.jedis.Jedis;

public class JedisDemo {
    public void execute() {

        Jedis jedis = new Jedis(); //创建客户端

        Boolean hello = jedis.exists("hello");
        System.out.println(hello);


        String s = jedis.get("hello");
        System.out.println(s);

        jedis.set("hello", "wrold:23");

        Long hello1 = jedis.exists("hello", "hello:123");
        System.out.println(hello1);


    }
}

 

测试:

package the_mass.redis;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        RedisService redisService = context.getBean(RedisService.class);
        redisService.testRedis();
        redisService.testRedisTemplate();
    }
}

 

 

注意:首先记得 设置值,不然会报空指针异常

 

1470521-20181225201149383-209306635.gifuploading.4e448015.gif转存失败重新上传取消

 

 
 
 
 
 
 
 
posted @ 2018-12-25 19:50 梓鸿 阅读(...) 评论(...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值