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();
    }
}

 

 

注意:首先記得 設置值,不然會報空指針異常

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM