京东的首页分类列表中的数据一般不会变动,在查询数据的过程中,可以把数据存储到内存中由redis管理,提高io效率。
分析:
用户通过浏览器第一次访问时redis中没有数据,先通过mysql等从硬盘中导入数据
第二次直接从redis中获取数据
实现:
按TDD开发
项目结构:
测试层:
test\java\jsu\lcw\service\TestCategoryService.java
public class TestCategoryService {
@Test
public void test01(){
//1,创建业务类对象
CategoryService categoryService = new CategoryService();
//2,查询分类集合
List<Category> list = categoryService.queryAll();
//3,显示数据
for(Category c:list){
System.out.println(c);
}
}
}
service层:
src\jsu\lcw\service\CategoryService.java
//商品服务类
public class CategoryService {
CategoryDao dao = new CategoryDao();
ObjectMapper objectMapper = new ObjectMapper();
//查询商品数据并返回
public List<Category> queryAll(){
//1,如果redis中有数据,JSON转list返回
//加载jedis,读取json数据
Jedis redis = JedisUtils.getJedis();
String json = redis.get("list");
if(json == null){
//1,如果redis中没有数据,从数据库中查找数据存入redis并返回。
System.out.println("第一次查询,从数据库中读取数据");
//1.1创建数据库对象,并从中读取数据
List<Category> list = dao.findAll();
String jsonValue = null;
try {
//1.2将数据转换成json
jsonValue = objectMapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//1.3将json数据存入redis
redis.set("list",jsonValue);
redis.close();
return list;
}else{
//2,如果redis中有数据,JSON转list返回
System.out.println("从redis中读取数据");
//2.1定义list对象用于存储数据
List<Category> list = null;//将json转成对象
try {
//2.2将redis中存储的json数据转成对应的list
list = objectMapper.readValue(json,new TypeReference<List<Category>>(){});
} catch (IOException e) {
e.printStackTrace();
}
redis.close();
return list;
}
}
}
工具包:
src\jsu\lcw\util\JedisUtils.java
public class JedisUtils {
private static JedisPool pool = null;
//1:创建一个连接池
static{
//1.1 解析properties文件
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
//获取参数
String maxTotal = bundle.getString("maxTotal");
String maxIdle = bundle.getString("maxIdle");
String url = bundle.getString("url");
String port = bundle.getString("port");
//1.2创建连接池
//1:创建连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//1.1 设置最大连接数
config.setMaxTotal(Integer.parseInt(maxTotal));
//1.2 设置空闲连接数
config.setMaxIdle(Integer.parseInt(maxIdle));
//2:创建连接池
pool = new JedisPool(config, url, Integer.parseInt(port));
}
//2:对外提供一个获取连接的方法
public static Jedis getJedis(){
return pool.getResource();
}
//3:提供释放资源的方法
public static void close(Jedis jedis){
if(jedis != null) {
jedis.close();
}
}
}
dao层:
src\jsu\lcw\dao\CategoryDao.java
public class CategoryDao {
//用list来模拟数据库
private List<Category> list = new ArrayList<>();
{
for(int i = 0; i < 10; i++){
list.add(new Category(i,"商品"+i,"类别"+i));
}
}
public List<Category> findAll() {
return list;
}
}
bean层:
src\jsu\lcw\bean\Category.java
public class Category {
private int id;
private String name;
private String cate;
......
filter层:
有中文解决中文编码
@WebFilter("/*")
public class FilterCharacter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
servlet层:
src\jsu\lcw\web\ServletList.java
@WebServlet("/list")
public class ServletList extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1,创建业务类对象
CategoryService categoryService = new CategoryService();
//2,查询分类集合
List<Category> list = categoryService.queryAll();
request.setAttribute("list",list);
System.out.println("service_list");
request.getRequestDispatcher("list.jsp").forward(request,response);
}
}
jsp页面:
<body>
<c:forEach items="${list}" var="item">
<div>${item.cate}</div>
</c:forEach>
</body>
导入:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>