访问redis数据库
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.domain.User;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, User> redisTemplate;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
// 保存字符串
|
||||
stringRedisTemplate.opsForValue().set("aaa", "111");
|
||||
Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
|
||||
|
||||
// 保存对象
|
||||
User user = new User("超人", 20);
|
||||
redisTemplate.opsForValue().set(user.getUsername(), user);
|
||||
|
||||
user = new User("蝙蝠侠", 30);
|
||||
redisTemplate.opsForValue().set(user.getUsername(), user);
|
||||
|
||||
user = new User("蜘蛛侠", 40);
|
||||
redisTemplate.opsForValue().set(user.getUsername(), user);
|
||||
|
||||
Assert.assertEquals(20, redisTemplate.opsForValue().get("超人").getAge().longValue());
|
||||
Assert.assertEquals(30, redisTemplate.opsForValue().get("蝙蝠侠").getAge().longValue());
|
||||
Assert.assertEquals(40, redisTemplate.opsForValue().get("蜘蛛侠").getAge().longValue());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user