redis 라이브러리 사용하기
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//라이브러리 pom.xml에 추가 하기
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
| cs |
. 환경설정
mybatis-context.xml 직접만들면
servlet-context.xml
방법1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// Servlet-context.xml 버전
<beans:bean id = "connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<beans:property name = "hostName"value="서버주소"/>
<beans: property name = "port"value="6379"/>
<beans: property name = "password"value="암호"/>
<beans: property name = "usePool"value="true"/>
</beans:bean>
<beans:bean id = "redisTemplate"class="org.springframework.data.redis.core.RedisTemplate">
<beans: property name = "connectionFactory"ref="connectionFactory"/>
| cs |
- 자료가 부족함.
새로운 라이브러리를 쓸때는 java 소스로 환경설정 하는 방법도 알아야 한다.
방법2.
새로운클래스를 만들어서 환경설정 매소드를 사용 한다. (여기서는 RedisConfig로 클래스를 만들어 주었다.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//JAVA 환경설정
@Configuration
public class RedisConfig {
//위처럼 만들기 java 버전 클래스화
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName("211.110.165.201");
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.setPassword("ds603");
jedisConnectionFactory.setUsePool(true);
return jedisConnectionFactory;
}
//참조 하는 내용이 있기때문에 이 내용은 무조건 아래에 있어야 한다.
@Bean
public StringRedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
StringRedisTemplate srt = new StringRedisTemplate();
srt.setConnectionFactory(jedisConnectionFactory);
return srt;
}
}
| cs |
===이번에는 기존 방식 대로 servlet-context에 라이브러리 환경설정을 추가했다.======
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
30
31
32
33
34
35
36
37
38
39
40
|
//redisController
redis 동작 확인
@Controller
public class RedisController {
@Autowired
RedisTemplate<String, String> redisTemplate;
@Autowired
RedisTemplate<String, Object> objectTemplate;
@Resource(name="redisTemplate")
private ValueOperations<String, String> value;
@RequestMapping(value = "/set_redis", method = RequestMethod.GET)
public String set_redis(){
value.set("jdh", "한글"); //세션서버에 값을 넣음
RedisVO rvo = new RedisVO();
rvo.setId("asdf");
rvo.setName("홍실대");
rvo.setAge(20);
objectTemplate.opsForValue().set("jdh1",rvo);
return "home";
}
@RequestMapping(value = "/get_redis", method = RequestMethod.GET)
public String get_redis(){
String str = value.get("jdh"); //세션 서버에 값을 가져옴
RedisVO rvo = (RedisVO)objectTemplate.opsForValue().get("jdh1");
System.out.println(str); //test1
System.out.println(rvo.getId());
System.out.println(rvo.getName());
System.out.println(rvo.getAge());
return "home";
}
}
| cs |
직렬화에 대한 설명 : http://lueseypid.tistory.com/42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//redisVO
public class RedisVO implements Serializable {
/**
직렬화하였다
*/
private static final long serialVersionUID = 1L;
private String id = null;
private String name = null;
private int age = 0;
//get,set만들어줌
| cs |
댓글 없음:
댓글 쓰기