Java代码操作Redis
1、Java访问redis
== 添加依赖==
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
Java连接redis
Jedis jedis = new Jedis(ip, port);
jedis.auth("123456");//权限认证
jedis.ping();
jedis.select(0);//切换数据库
Java操作redis
string(字符串)
hash(哈希)
list(列表)
set(集合)
zset(sorted set:有序集合)
zadd/zrevrange
代码测试Demo
package com.DZY.idea;
import redis.clients.jedis.Jedis;
import java.lang.reflect.Field;
import java.util.Map;
/**
* @author DZY
* @site www.1620797557.com
* @company 卓京
* @create 2019-11-12 10:49
*/
public class Demo {
private String user;
public static void main(String[] args) {
Jedis jedis=new Jedis("192.168.16.130",6379);
jedis.auth("123456");
//校验redis服务正常 然后通过jedis连接服务正常
//System.out.println(jedis.ping());
//操作String
//jedis.set("sname","立冬");
//System.out.println(jedis.get("sname"));
//操作hash
//所以的值都在user中
/*User user=new User("寒冷","1234","多穿衣服");
for (Field field : user.getClass().getDeclaredFields()) {
field.setAccessible(true);
try {
jedis.hset("user",field.getName(),field.get(user).toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}*/
//System.out.println(jedis.hget("user","us"));
/*Map<String,String> user=jedis.hgetAll("user");
for (Map.Entry<String,String> entry:user.entrySet()){
System.out.println(entry.getKey()+" : "+entry.getValue());
}*/
//操作List
jedis.lpush("love","1","2","3","4","5","6");
System.out.println(jedis.lpop("love"));
System.out.println(jedis.rpop("love"));
}
}
反射需要用的User类
package com.DZY.idea;
/**
* @author DZY
* @site www.1620797557.com
* @company 卓京
* @create 2019-11-12 10:50
*/
public class User {
private String uname;
private String upwd;
private String us;
public User() {
}
public User(String uname, String upwd, String us) {
this.uname = uname;
this.upwd = upwd;
this.us = us;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String getUs() {
return us;
}
public void setUs(String us) {
this.us = us;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
1、首页第一次是读取数据库,后面读取缓存(在没有增删改的情况)
导入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
==BookServler ==
package com.DZY.idea;
import redis.clients.jedis.Jedis;
import javax.print.DocPrintJob;
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;
/**
* @author DZY
* @site www.1620797557.com
* @company 卓京
* @create 2019-11-12 10:50
*/
@WebServlet("/list")
public class BookServler 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 {
//先从redis里面取书籍的前十条数据
Jedis jedis=new Jedis("192.168.16.130",6379);
jedis.auth("123456");
//字符串bookList存放的是 json数组字符串 List<Book> --->json
//如果需要通过json数组传做jsp页面展示的话 需要将json数据字符串 -->list<book> jackSon
String bookList=jedis.get("bookList");
if(null == bookList || "".equals("bookList")){
//此时意味着是第一次查询 那么需要走数据库mysql
System.out.println("先走数据库做书籍列表的查询");
String list ="从mysql数据库中查询出十条数据 再转换成json串";
jedis.set("bookList",list);
req.setAttribute("jspList","数据来源于mysql"+jedis.get("bookList"));
}else {
//意味着不是第一次查询 那么就可以将数据返回了
req.setAttribute("jspList","数据来源于redis"+bookList);
}
req.getRequestDispatcher("bookList.jsp").forward(req,resp);
}
}
bookList.jsp
<%--
Created by IntelliJ IDEA.
User: wt
Date: 2019/11/13
Time: 19:53
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>
书籍列表展示数据:${jspList}
</body>
</html>
运行:第一次当redis没有值的时候
第二次或多次
2、增删改的时候,要顺带更新缓存,下一次再次访问首页要保证redis中数据跟mysql数据是一致
核心代码BookAction
package com.DZY.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.DZY.dao.BookDao;
import com.DZY.entity.Book;
import com.DZY.framework.ActionSupport;
import com.DZY.framework.ModelDriven;
import com.DZY.util.PageBean;
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book=new Book();
private BookDao bookDao=new BookDao();
public Jedis redisList(){
Jedis jedis = new Jedis("192.168.16.130);
jedis.auth("xwt@429XWT..");
return jedis;
}
/**
* 查询
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req,HttpServletResponse resp) {
PageBean pageBean =new PageBean();
pageBean.setRequest(req);
try {
List<Book> list=this.bookDao.list(book, pageBean);
req.setAttribute("bookList", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
/**
* 跳转新增修改页面
* @param req
* @param resp
* @return
*/
public String preSav(HttpServletRequest req,HttpServletResponse resp) {
if(book.getBid()!=0) {
try {
//数据回显的数据
Book b=this.bookDao.list(book, null).get(0);
req.setAttribute("book", b);
} catch (Exception e) {
e.printStackTrace();
}
}
return "edit";
}
/**
* 增加
* @param req
* @param resp
* @return
* @throws Exception
*/
public String add(HttpServletRequest req,HttpServletResponse resp) throws Exception {
try {
this.bookDao.add(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toList";
}
/**
* 修改
* @param req
* @param resp
* @return
* @throws Exception
*/
public String edit(HttpServletRequest req,HttpServletResponse resp) throws Exception {
try {
this.bookDao.edit(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toList";
}
/**
* 删除
* @param req
* @param resp
* @return
* @throws Exception
*/
public String del(HttpServletRequest req,HttpServletResponse resp) throws Exception {
try {
this.bookDao.del(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toList";
}
@Override
public Book getModel() {
return book;
}
}
从数据库获取数据到页面的时间
从redis获取数据到页面的时间