1.打开服务
2.导入jar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
3.在spring中进行资源配置
<!--配置Redis连接池-->
<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
<!--最大空闲时间-->
<property name="maxWaitMillis" value="1000"></property>
<!--最大连接数-->
<property name="maxTotal" value="1000"></property>
<!--最大持有数-->
<property name="maxIdle" value="50"></property>
</bean>
<!--注入jedis对象-->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<!--注入连接池属性-->
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
<!--注入IP地址-->
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<!--注入端口号-->
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
4.在Controller中自动装配
@Autowired
private JedisPool jedisPool;
5.调用
@Controller
@RequestMapping("/class")
public class ClassinfoController {
@Autowired
private ClassinfoDao classinfoDao;
@Autowired
private JedisPool jedisPool;
@RequestMapping(value = "/findAll" , produces = "json/text;charset=utf-8")
@ResponseBody
String findAll(){
// jedisPool.getResource().select(3);
String info=jedisPool.getResource().get("classinfo");
System.out.println(info);
if (info == null){
List<Classinfo> lists= classinfoDao.findAll();
System.out.println(JSONObject.toJSONString(lists));
jedisPool.getResource().select(3);
jedisPool.getResource().set("classinfo",JSONObject.toJSONString(lists));
info=JSONObject.toJSONString(lists);
}
return info;
}
}
6.配置jsp页面
<%@ page pageEncoding="UTF-8" %>
<html>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<body>
<h2>Hello World!</h2>
<table id="info">
<tr>
<td>班级序号</td>
<td>班级名称</td>
</tr>
</table>
<script>
$(function () {
$.ajax({
url:'class/findAll',
dataType:'json',
success:function (data) {
console.log(data)
for (var i = 0; i <data.length ; i++) {
$("#info").append("<tr><td>"+data[i].classid+"</td><td>"+data[i].className+"</td></tr>")
}
}
})
})
</script>
</body>
</html>
7.最终效果
|