1. 程式人生 > >Spring Boot開發系列(Redis)(三)--Spring boot 整合redis完成簡單的get,set操作

Spring Boot開發系列(Redis)(三)--Spring boot 整合redis完成簡單的get,set操作

Spring Boot開發系列(Redis)(三)–Spring boot 整合redis完成簡單的get,set操作

【1】匯入reids相關依賴

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

【2】Reids相關配置

#jedis相關配置
spring.redis.host=192.168.132.132
spring.redis.port=6379
spring.redis.password=abc123
spring.redis.timeout=0

【3】編寫啟動主函式

package com.sun.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(value = "com.sun.springboot.mapper")
@SpringBootApplication
public class MyWebService {

   public static void main(String[] args) {
      SpringApplication.run(MyWebService.class, args);
   }
}

【4】編寫測試函式,完成簡單的get,set命令操作redis

package com.sun.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RunWith(SpringRunner.class)
@SpringBootTest

public class SpringBootMyServiceApplicationTests {
	@Autowired
   	JedisPool jedisPool;
   	Logger logger = LoggerFactory.getLogger(getClass());
   	//測試jedis操作redis快取
	@Test
   	public void setKeyValueTest(){
       		Jedis jedis = jedisPool.getResource();
       		logger.info("set key-value info in redis...");
       		jedis.set("Key1","Value1");
	}
	@Test
	public void getKeyValueTest(){
    		Jedis jedis = jedisPool.getResource();
    		logger.info("get key-value info in redis...");
    		if(jedis.get("Key1") == null){
       	 	logger.info("No key-value info in redis!");
    	}else{
        	logger.info(jedis.get("Key1"));
	}
}

【5】常見問題 (1). SpringBoot引入Redis報org.springframework.data.redis.core.RedisTemplate類找不到錯誤 解決:在pom.xml中加入

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.1.RELEASE</version>
</dependency>

(2). ‘dependencies.dependency.version’ is missing for xxx 原因與解決 maven 對於父專案定義的dependencyManagement 中的 非 jar 型別的<type></type>節點不會繼承,也就是說子專案中必須再次宣告非 jar 型別的<type></type>即可。 示例程式碼:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version> 1.5.1.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
                <version>1.5.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.7.1.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
    </dependencies>

(3). org.springframework.boot:spring-boot-starter-redis:jar is missing 解決:在pom.xml中加入

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

致謝參考: 本部落格為博主的學習筆記及經驗總結,同時結合學習了網上眾多博主的部落格,在此表示感謝。