发表于: 2020-09-06 20:34:26
0 1325
把首页的jsp页面的所有数据都加上了缓存
//带缓存
@RequestMapping(value = "/MemhomePage", method = RequestMethod.GET)
public String MemallStudent(Model model) {
try {
List<student> students = studentService.MemAllStudent();
List<company> company = companyService.MemAllCompany();
int countStudent = studentService.MemCountStudent();
int countCompany = companyService.MemCountCompany();
int countSatisfiedSalary = studentService.MemCountSatisfiedSalary();
model.addAttribute("students", students);
model.addAttribute("company", company);
model.addAttribute("countStudent", countStudent);
model.addAttribute("countCompany", countCompany);
model.addAttribute("countSatisfiedSalary", countSatisfiedSalary);
return "homePage";
} catch (Exception e) {
return "index";
}
}
5个方法
companyServiceImpl 俩
@Override
public List<company> MemAllCompany(){
//取出所有学生的缓存
List<company> company = (List<company>) client.get("AllCompany");
logger.info("查询出的缓存值为" + company);
if (company != null) {
logger.info("返回缓存");
return company;
} else {
logger.info("缓存为空,现在从数据库查询数据");
List<company> companys = companyMapper.allCompany();
logger.info("从数据库中查询处的数据为" + companys);
logger.info("把数据放入缓存");
boolean state = client.set("AllCompany",companys);
logger.info("添加新缓存是否成功" + state);
return companys;
}
}
//带缓存的countCompany
@Override
public int MemCountCompany() {
try{
int rs = (int) client.get("countCompany");
logger.info("有缓存,缓存为"+rs+"现在开始返回缓存");
return rs;
}catch (Exception e){
//如果缓存为空 返回缓存数据
logger.info("没有缓存,现在从数据库查询数据");
int rss = companyMapper.countCompany();
logger.info("从数据库中查询处的数据为" + rss);
logger.info("现在开始把数据放入缓存");
boolean state = client.set("countCompany", rss);
logger.info("添加新缓存是否成功" + state);
return rss;
}
}
studentServiceImpl 仨
//不带缓存的countStudent
@Overridepublic int countStudent() {
return studentMapper.countStudent(); }
//带缓存的countStudent
@Override
public int MemCountStudent() {
try{
int rs = (int) client.get("countStudent");
logger.info("有缓存,缓存为"+rs+"现在开始返回缓存");
return rs;
}catch (Exception e){
//如果缓存为空 返回缓存数据
logger.info("没有缓存,现在从数据库查询数据");
int rss = studentMapper.countStudent();
logger.info("从数据库中查询处的数据为" + rss);
logger.info("现在开始把数据放入缓存");
boolean state = client.set("countStudent", rss);
logger.info("添加新缓存是否成功" + state);
return rss;
}
}
//不带缓存
@Override
public int countSatisfiedSalary() {
return studentMapper.countSatisfiedSalary();
}
//带缓存
@Override
public int MemCountSatisfiedSalary() {
try{
int rs = (int) client.get("countSalary");
logger.info("有缓存,缓存为"+rs+"现在开始返回缓存");
return rs;
}catch (Exception e){
//如果缓存为空 返回缓存数据
logger.info("没有缓存,现在从数据库查询数据");
int rss = studentMapper.countSatisfiedSalary();
logger.info("从数据库中查询处的数据为" + rss);
logger.info("现在开始把数据放入缓存");
boolean state = client.set("countSalary", rss);
logger.info("添加新缓存是否成功" + state);
return rss;
}
}
//不带缓存
@Override
public List<student> allStudent() {
return studentMapper.allStudent();
}
//带缓存
@Override
public List<student> MemAllStudent() {
//取出所有学生的缓存
List<student> student = (List<student>) client.get("student");
logger.info("查询出的缓存值为" + student);
if (student != null) {
logger.info("返回缓存");
return student;
} else {
logger.info("缓存为空,现在从数据库查询数据");
List<student> students = studentMapper.allStudent();
logger.info("从数据库中查询处的数据为" + students);
try {
logger.info("把数据放入缓存");
boolean state = client.set("student", students);
logger.info("添加新缓存是否成功" + state);
}catch (Exception e){
e.printStackTrace();
}
return students;
}
}
这个逻辑写完
报错排半天
想着很简单,但写起来 就出问题。
其中有个空指针异常错误困扰了我好久
逻辑我是这么写的
int rs = (int) client.get("countCompany");
if(rs!=0){
返回缓存
}else{
查数据库
添加缓存
}
错误原因: int数据不能为null
所以假如没有获取缓存 就报空指针异常
今日问题 暂无
评论