使用java代码操作redis

使用 java 代码操作 Redis

1. Java 访问 redis

  2.1 添加依赖

      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
      </dependency>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6 
 7   <groupId>com.yuan</groupId>
 8   <artifactId>redis01</artifactId>
 9   <version>1.0-SNAPSHOT</version>
10   <packaging>war</packaging>
11 
12   <name>redis01 Maven Webapp</name>
13   <!-- FIXME change it to the project's website -->
14   <url>http://www.example.com</url>
15 
16   <properties>
17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18     <maven.compiler.source>1.7</maven.compiler.source>
19     <maven.compiler.target>1.7</maven.compiler.target>
20   </properties>
21 
22   <dependencies>
23     <dependency>
24       <groupId>junit</groupId>
25       <artifactId>junit</artifactId>
26       <version>4.12</version>
27       <scope>test</scope>
28     </dependency>
29     <dependency>
30       <groupId>redis.clients</groupId>
31       <artifactId>jedis</artifactId>
32       <version>2.9.0</version>
33     </dependency>
34   </dependencies>
35 
36   <build>
37     <finalName>redis01</finalName>
38     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
39       <plugins>
40         <plugin>
41           <artifactId>maven-clean-plugin</artifactId>
42           <version>3.1.0</version>
43         </plugin>
44         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
45         <plugin>
46           <artifactId>maven-resources-plugin</artifactId>
47           <version>3.0.2</version>
48         </plugin>
49         <plugin>
50           <artifactId>maven-compiler-plugin</artifactId>
51           <version>3.8.0</version>
52         </plugin>
53         <plugin>
54           <artifactId>maven-surefire-plugin</artifactId>
55           <version>2.22.1</version>
56         </plugin>
57         <plugin>
58           <artifactId>maven-war-plugin</artifactId>
59           <version>3.2.2</version>
60         </plugin>
61         <plugin>
62           <artifactId>maven-install-plugin</artifactId>
63           <version>2.5.2</version>
64         </plugin>
65         <plugin>
66           <artifactId>maven-deploy-plugin</artifactId>
67           <version>2.8.2</version>
68         </plugin>
69       </plugins>
70     </pluginManagement>
71   </build>
72 </project>

 

  2.2 Java 连接 redis

      Jedis jedis = new Jedis(ip, port);
      jedis.auth("123456");// 权限认证
      jedis.ping();
      jedis.select(0);// 切换数据库
package com.yuan;

import redis.clients.jedis.Jedis;

import java.util.Map;

/**

  • 讲解 java 代码操作 redis
  • String、hash、list
  • 1、加载驱动
  • 2、建立连接(url/uname/pwd)
  • 3、preparestatement
  • 4、执行 sql
  • 5、处理结果
  • 6、关闭资源
  • redis 连接步骤
  • 1、建立连接,连接后授权
  • 2、使用 redis

*/
public class Demo1 {
public static void main(String[] args) {
//连接 redis
Jedis jedis = new Jedis("192.168.238.129",6379);
jedis.auth(
"123456");
System.out.println(jedis.ping());

}

}

 

  2.3 Java 操作 redis

      string(字符串)
      hash(哈希)
      list(列表)

      注 1:不需要记得 API 的方法,只需要查 redis 命令
package com.yuan;

import redis.clients.jedis.Jedis;

import java.util.Map;

/**

  • 讲解 java 代码操作 redis
  • String、hash、list
  • 1、加载驱动
  • 2、建立连接(url/uname/pwd)
  • 3、preparestatement
  • 4、执行 sql
  • 5、处理结果
  • 6、关闭资源
  • redis 连接步骤
  • 1、建立连接,连接后授权
  • 2、使用 redis

/
public class Demo1 {
public static void main(String[] args) {
//连接 redis
Jedis jedis = new Jedis("192.168.238.129",6379);
jedis.auth(
"123456");
System.out.println(jedis.ping());
/

string
/
// jedis.set("name","zs");
// jedis.set("age","22");
// System.out.println(jedis.get("name"));
/

hash
*/
// jedis.hset("user1","uname","ls");
// jedis.hset("user1","pwd","123456");
// jedis.hset("user1","sex","nv");
// System.out.println(jedis.hget("user1","uname"));
// Map<String,String> user1 = jedis.hgetAll("user1");
// for (Map.Entry<String, String> entry :user1.entrySet()){
// System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
// }

    <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    list
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

// jedis.lpush("hobby","a","b","c","d","e","f","g");
// System.out.println(jedis.lpop("hobby"));
// System.out.println(jedis.rpop("hobby"));

}
}

 

 DemoServlet
 1 package com.yuan;
 2 
 3 import redis.clients.jedis.Jedis;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import java.io.IOException;
11 
12 
13 @WebServlet("/list")
14 public class DemoServlet extends HttpServlet {
15     @Override
16     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17         doPost(req,resp);
18     }
19 
20     @Override
21     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22         // 首页第一次是读取数据库,后面读取缓存(在没有增删改的情况)
23         //首页第一次是读取数据库,后面读取缓存
24         Jedis jedis=new Jedis("192.168.47.129",6379);
25         jedis.auth("123456");
26 
27         String booklist = jedis.get("booklist");
28         if(booklist==null || "".equals(booklist)){
29             //查询数据库
30             String mysqlData="data";
31             //将 mysqldata 转成 json 数组串
32             jedis.set("booklist",mysqlData);
33             booklist = jedis.get("booklist");
34             //第一次登录,访问数据库
35             req.setAttribute("mag","从数据库拿数据");
36             req.setAttribute("booklist",booklist);
37             req.getRequestDispatcher("/booklist.jsp").forward(req,resp);
38         }else{
39             //从缓存获取当前信息
40             req.setAttribute("mag","直接从 redis 中取得数据");
41             req.setAttribute("booklist",booklist);
42             req.getRequestDispatcher("/booklist.jsp").forward(req,resp);
43         }
44     }
45 }

 booklist.jsp 代码

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: 刘源
 4   Date: 2019/10/14
 5   Time: 1:52
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <%@ page isELIgnored="false" %>
10 <html>
11 <head>
12     <title>Title</title>
13 </head>
14 <body>
15 ${mag}:${booklist}
16 </body>
17 </html>

 

 谢谢观看!!!如需跟详细的讲解请百度查找 redis 的教程。