1、redis的基本Java操作
准备工作
将VMware Workstation Pro登录启动后就设置在后台运行
然后打开redis数据库
jedis连接
Jedis jedis = new Jedis("192.168.198.128",6379);//指定Redis服务Host和port
jedis.auth("123456");//连接密码
System.out.println(jedis.ping());
连接成功了
操作字符串
jedis.set("aa","zs");
System.out.println(jedis.get("aa"));
操作哈希
存储哈希
jedis.hset("user1","name","阿豆");
jedis.hset("user1","sex","男");
取哈希
System.out.println(jedis.hgetAll("user1"));//取所有
System.out.println(jedis.hget("user1", "name"));//取名字
操作列表list
jedis.lpush("hobby","a","b","c","d","e");
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.rpop("hobby"));
先进后出
2、redis应用到查询中
导入依赖servlet
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
首页home.jsp
<%--
Created by IntelliJ IDEA.
User: zrh
Date: 2019/9/19
Time: 20:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>博客首页</title>
</head>
<body>
博客首页
拿取数据的方式:${msg}<br>
拿去到的数:${currentUser}
</body>
</html>
DemoServlet
package com.zrh;
import redis.clients.jedis.Jedis;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/**
* @author zrh
* @site IDEA项目
* @company
* @create 2019-09-1920:51
*/
@WebServlet("/getData")
public class DemoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 首页第一次是读取数据库,后面读取缓存(在没有增删改的情况)
Jedis jedis = new Jedis("192.168.198.128",6379);//指定Redis服务Host和port
jedis.auth("123456");//连接密码
// 从缓存中获取当前登录的用户信息
Map<String,String> currentUser = jedis.hgetAll("currentUser");
if(currentUser !=null && currentUser.size()>0){
req.setAttribute("msg","从缓存中获取数据");
req.setAttribute("currentUser",currentUser);
}
else{
// 第一次登录,第一次访问首页数据
req.setAttribute("msg","从数据库中获取数据");
String name = "zhangsan";
String pass = "123456";
// 接下来把数据库中的对象存储到缓存中去
jedis.hset("currentUser","name","zhangsan");
jedis.hset("currentUser","pass","123456");
// 此时能获取到值原因是上面已经将数据存储到缓存中
currentUser = jedis.hgetAll("currentUser");
req.setAttribute("currentUser",currentUser);
}
req.getRequestDispatcher("home.jsp").forward(req,resp);
}
}
界面展示
注意Idea下JDK版本和Tomcat版本冲突:
报错信息
19-Sep-2019 20:17:06.984 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method createStandardContext
javax.management.RuntimeOperationsException: Exception invoking method manageApp
解决方案:
jdk:1.8
Tomcat:tomcat-9.0.14
将Tomacat版本退到:tomcat-8.0.23问题解决;
tomcat-8.0.23压缩包百度云资源:tomcat-8.0的地址