**
控制类
**
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.bdqn.entity.Grade;
import com.bdqn.entity.student;
import com.bdqn.service.ServiceDao;
import com.bdqn.service.StudentServiceDao;
import com.bdqn.util.ToolUtil;
import com.mysql.jdbc.Field;
import redis.clients.jedis.Jedis;
@Controller
@RequestMapping("student")
public class studentContorller {
@Autowired
StudentServiceDao stu;
@Autowired
ServiceDao grade;
@Autowired
Jedis jedis;//在下面展示配置
@RequestMapping("list")
public String list(@RequestParam(defaultValue="1")int index,@RequestParam(defaultValue="0")int gradeId,String name,Model model,HttpServletRequest request) {
//SpringMVC中取Session
HttpSession session = request.getSession();
ToolUtil<student> list=null;
//在Session中封装每一个用户的查询条件。如果有条件,则存储条件
String key="student"+index;
if(jedis.exists(key)) {
System.out.println("缓存获取");
String str=jedis.get(key);
list=JSON.parseObject(str,ToolUtil.class);
}else {
System.out.println("数据库获取");
list=stu.page(index, 4,gradeId,name);
String json=JSON.toJSONString(list);
jedis.set(key, json);
}
String key1="student"+index+gradeId;
if(jedis.exists(key1)) {
System.out.println("缓存获取");
String str=jedis.get(key1);
list=JSON.parseObject(str,ToolUtil.class);
}else {
System.out.println("数据库获取");
if(gradeId != 0) {
session.setAttribute("gradeId", gradeId);
}
else { //如果没有条件,则看Session中有没有条件。
if(session.getAttribute("gradeId") != null)
gradeId = (int)session.getAttribute("gradeId");
}
list=stu.page(index, 4,gradeId,name);
String json=JSON.toJSONString(list);
jedis.set(key1, json);
}
String key2="student"+index+name;
if(jedis.exists(key2)) {
System.out.println("缓存获取");
String str=jedis.get(key2);
list=JSON.parseObject(str,ToolUtil.class);
}else {
System.out.println("数据库获取");
if(name != null) {
session.setAttribute("name", name);
}
else {
if(session.getAttribute("name") != null)
name = (String) session.getAttribute("name");
}
list=stu.page(index, 4,gradeId,name);
String json=JSON.toJSONString(list);
jedis.set(key2, json);
}
String key3="student"+index+name+gradeId;
if(jedis.exists(key3)) {
System.out.println("缓存获取");
String str=jedis.get(key3);
list=JSON.parseObject(str,ToolUtil.class);
}else {
System.out.println("数据库获取");
if(gradeId != 0) {
session.setAttribute("gradeId", gradeId);
}
else { //如果没有条件,则看Session中有没有条件。
if(session.getAttribute("gradeId") != null)
gradeId = (int)session.getAttribute("gradeId");
}
if(name != null) {
session.setAttribute("name", name);
}
else {
if(session.getAttribute("name") != null)
name = (String) session.getAttribute("name");
}
list=stu.page(index, 4,gradeId,name);
String json=JSON.toJSONString(list);
jedis.set(key3, json);
}
List<Grade> lists=grade.selectAll();
model.addAttribute("grade",lists);
model.addAttribute("students",list);
model.addAttribute("gradeId", gradeId);
model.addAttribute("name", name);
return "studentList";
}
jedis配置
<bean id="jedis" class="redis.clients.jedis.Jedis">
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6379" />