`

SpringBoot Redis配置(八)

 
阅读更多

1、添加redis依赖

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

 2、增加配置application.properties

#redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
#最大连接数
spring.redis.pool.max-active=10000
#最大控件连接数
spring.redis.pool.max-idle=5

 3、工具类

package com.zzstxx.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * redis工具类
 * @author zxf
 *
 */
@Component
public class RedisDao {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	public void set(String key, String value) {
		this.stringRedisTemplate.opsForValue().set(key, value);
	}

	public String get(String key) {
		return this.stringRedisTemplate.opsForValue().get(key);
	}

	public void delete(String key) {
		this.stringRedisTemplate.delete(key);
	}
}

 4、测试类

package com.zzstxx.sysuser;

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.test.context.junit4.SpringRunner;

import com.zzstxx.redis.RedisDao;

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

	@Autowired
	private RedisDao redisDao;

	@Test
	public void testSet() {
		String key = "name";
		String value = "zhangsan";
		this.redisDao.set(key, value);
		String value1 = this.redisDao.get(key);
        System.out.println(value1);
	}
	
	/*@Test
    public void testGet() {
        String key = "name";
        String value = this.redisDao.get(key);
        System.out.println(value);
    }
    
    @Test
    public void testDelete() {
        String key = "name";
        this.redisDao.delete(key);
    }*/
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics