1. 功能分析
1.1 login.html中的header.html
- 发送ajax请求,访问服务器,加载真正的分类数据
- 遍历数组,完成展示
1.2 web层
- 调用service查询返回list返回展示
1.3 service层
- 先在redis缓存查询,没有则访问mysql查询,并加入redis,有则不需要访问mysql
2. header.html
$.get("/ssm-travel/user/findName", {}, function (data) {......});
$.get("/ssm-travel/category/selectCategoryAll", {}, function (data) {
// [{"cid":8,"cname":"全球自由行"},{"cid":5,"cname":"国内游"},{"cid":4,"cname":"处境游"}]
var lis = '<li class="nav-active"><a href="index.html">首页</a></li>';
// 遍历数组
for (var i = 0; i < data.length; i++) {
var li = '<li><a href="route_list.html?cid=' + data[i].cid + '">' + data[i].cname + '</a></li>'
lis += li
}
// 加入收藏排行榜
lis += '<li><a href="favoriterank.html">收藏排行榜</a></li>'
console.log(lis)
$("#category").html(lis);
})
3. mapper层
- CategoryMapper.java
src\main\java\com\ssmtravel\mapper\CategoryMapper.java
package com.ssmtravel.mapper;
import com.ssmtravel.domain.Category;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface CategoryMapper {
// 判断用户是否已经存在
@Select("select * from tab_category")
List<Category> CategoryData();
}
3. service层
- CategoryService.jave 接口
package com.ssmtravel.service;
import com.ssmtravel.domain.Category;
import java.util.List;
public interface CategoryService {
// 判断用户是否已经存在
List<Category> CategoryData();
}
- 实现 CategoryServiceImpl.java
package com.ssmtravel.service.Impl;
import com.ssmtravel.domain.Category;
import com.ssmtravel.mapper.CategoryMapper;
import com.ssmtravel.service.CategoryService;
import com.ssmtravel.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public List<Category> CategoryData() {
//1. 先在redis缓存查询,没有则访问mysql查询,并加入redis,有则不需要访问mysql
// 1.1 redis缓存查询
// 1.2 获取jedis客户端
Jedis jedis = JedisUtil.getJedis();
// 1.3可使用sortedest排序查询
Set<Tuple> categorys = jedis.zrangeWithScores("category", 0, -1);
List<Category> cs = null;
// 1.4 判断查询的集合是否为空
if (categorys == null || categorys.size() == 0) {
//System.out.println("从数据库查询");
// 1.5为空表示需要从数据库查询,再存入缓存
// 数据库查询
cs = categoryMapper.CategoryData();
// 将数据存入redis
for (int i = 0; i < cs.size(); i++) {
jedis.zadd("category", cs.get(i).getCid(), cs.get(i).getCname());
}
} else {
//System.out.println("从redis中查询");
// 1.6. 不为空,从redis中查询,将set集合的数据存入 list中
cs = new ArrayList<Category>();
for (Tuple tuple : categorys) {
Category category = new Category();
category.setCname(tuple.getElement());
category.setCid((int) tuple.getScore());
cs.add(category);
}
}
return cs;
}
}
4.web层
- CategoryContorller.java
package com.ssmtravel.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ssmtravel.domain.Category;
import com.ssmtravel.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
/**
* @title: UserContorller
* @Author Qian
* @Date: 2022/4/14 16:09
* @Version 1.0
*/
@Controller
@RequestMapping("/category")
public class CategoryContorller {
private String err_msg = null;
@Autowired
private CategoryService categoryService;
// 1. 分类数据展示
@RequestMapping(value = "/selectCategoryAll",produces = "application/json;charset=utf-8")
@ResponseBody
public String exitLogin(HttpSession session) throws IOException {
List<Category> categories = categoryService.CategoryData();
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(categories);
}
}
5. 工具类
- JedisUtil.java
package com.ssmtravel.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Jedis工具类
*/
public final class JedisUtil {
private static JedisPool jedisPool;
private static String AUTH;
static {
//读取配置文件
InputStream is = JedisPool.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties pro = new Properties();
//关联文件
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
// 将AUTH赋值,如果redis没有密码,则不需要此行代码
AUTH=pro.getProperty("auth");
//获取数据,设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
//初始化JedisPool
jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
// jedisPool = new JedisPool(config, "localhost", 6379, -1, "123456");
}
/**
* 获取连接方法
*/
public static Jedis getJedis() {
Jedis jedis = jedisPool.getResource();
jedis.auth(AUTH);
return jedis;
}
/**
* 关闭Jedis
*/
public static void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
- redis配置文件:jedis.properties
host=127.0.0.1
port=6379
auth=123456
maxTotal=50
maxIdle=10