基本准备

  1. 本地通过brew安装redis
  2. 安装一个redis的可视化管理工具
    我安装的是Redis Desktop Manager for Mac

#工程配置

新建配置文件

redis.properties

1
2
3
4
5
# redis
redis.host=localhost
redis.port=6379
redis.password=
redis.database=1

注入数据源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Configuration
@PropertySource("classpath:config/redis.properties")
public class RedisConfig {

@Autowired
private Environment env;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(env.getProperty("redis.host").trim());
jedisConnectionFactory.setPort(Integer.parseInt(env.getProperty("redis.port").trim()));
jedisConnectionFactory.setPassword(env.getProperty("redis.password").trim());
jedisConnectionFactory.setDatabase(Integer.parseInt(env.getProperty("redis.database").trim()));
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
return jedisConnectionFactory;
}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Repository
public class RedisBaseDao {

@Resource(name="redisTemplate")
protected ValueOperations<String, String> valueOperations;

public void addValue(String key, String value){
valueOperations.set(key, value);
}

public String getValue(String key){
return valueOperations.get(key);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Repository
public class ValueRedisDao {
@Autowired
public RedisBaseDao redisBaseDao;

private String getKey(){
return "param";
}

public void save(String param){
this.redisBaseDao.addValue(this.getKey(), param);
}

public String getParam(){
return this.redisBaseDao.getValue(this.getKey());
}
}

测试类进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@RunWith(SpringRunner.class)
@SpringBootTest
public class WpspringbootmybatisApplicationTests {

@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void test() throws Exception {
// 保存字符串
stringRedisTemplate.opsForValue().set("aaa", "111");
Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
}

@Autowired
private ValueRedisDao valueRedisDao;

@Test
public void saveRedisTest() throws Exception {
this.valueRedisDao.save("LiangGzone");
System.out.println(this.valueRedisDao.getParam());
}


@Test
public void readRedisTest() throws Exception{
String value=valueRedisDao.getParam();
System.out.println(value);
}
}

结合可视化软件,可以确定redis的操作

不过这样的使用还是比较初级,之后结合公司的项目进行封装