文章目录

涉及到spring配置redis 和示例代码

applicationContext.xml 这里网上有许多版本 ,但是copy过来不是版本不符就是各种找不到类

用到的版本

jedis-2.6.0.jar

spring-data-redis-1.4.1.RELEASE.jar

对应spring集成的版本是4.0

下面是 applicationContext.xml

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
41
42
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"
p:host-name="111.111.111.111" p:port="6379" p:password="abc" />
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory">
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="stringSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>

下面是比较重要的Controller层实现的代码

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.controller;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.service.TestService;
import com.util.alibaba.fastjson.JSON;
import com.util.alibaba.fastjson.JSONObject;
@Controller
public class TestController {
@Autowired
private TestService testService;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@RequestMapping("/index")
public ModelAndView index(HttpServletRequest request,
HttpServletResponse response, HttpSession session,
String pageNo,String pageCount, String name,
ModelMap modelMap) throws IOException, ParseException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
JSONObject json=new JSONObject();
if (pageNo == null || pageNo.equals("") || pageCount == null
|| pageCount.equals("")) {
pageNo = "1";
pageCount = "10";
}
if ((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageCount)>210) {
pageNo="21";
pageNo="10";
}
if (name!=null&&!name.equals("")) {
if (name.contains("%")) {
modelMap.put("name", URLEncoder.encode(name, "UTF-8"));
name=URLDecoder.decode(URLDecoder.decode(name, "UTF-8"), "UTF-8");
}else {
modelMap.put("name", URLEncoder.encode(URLEncoder.encode(name, "UTF-8"), "UTF-8"));
}
modelMap.put("yname", URLDecoder.decode(name, "UTF-8"));
}else{
modelMap.put("name", "");
name="";
}
String tmpname=name;
if (tmpname.equals("")) {
tmpname="AllSerachResult";
}
Object redisname=redisTemplate.opsForValue().get(tmpname);
if (redisname!=null&&redisname.toString().length()>2) {
String Strname=redisname.toString().trim();
json=JSON.parseObject(Strname);
modelMap.addAllAttributes(json);
return new ModelAndView("/news", modelMap);
}
modelMap.put("pageNo", Integer.parseInt(pageNo));
modelMap.put("pageCount", Integer.parseInt(pageCount));
Long totleCount = testService.getCatsByUserCount(name);//TODO
if (totleCount % 10 != 0) {
totleCount = totleCount / 10 + 1;
} else {
totleCount = totleCount / 10;
}
List<Integer> totleCountList = new ArrayList<Integer>();
for (int i = 0; i < totleCount; i++) {
totleCountList.add(i + 1);
}
modelMap.put("totleCountList", totleCountList);
List<Map> list = testService.getCatsByUser(name,pageNo,pageCount);//TODO
modelMap.put("list", list);
json.putAll(modelMap);
redisTemplate.opsForValue().set(tmpname,json.toJSONString(), 10);
return new ModelAndView("/news", modelMap);
}
}

文章目录