Java访问redis
添加依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.MavenRedis</groupId> <artifactId>RedisProject</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>RedisProject Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies> <build> <finalName>RedisProject</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build></project>
Java连接redis
1 Jedis jedis = new Jedis(ip, port);
2 jedis.auth("123456");//权限认证
3 jedis.ping();
4 jedis.select(0);//切换数据库
使用java代码操作redis
查询中使用redis的逻辑

edis在增删改中的使用

存值取值
1 string(字符串) 2 hash(哈希) 3 list(列表) 4 set(集合) 5 zset(sorted set:有序集合) 6 zadd/zrevrange
Demo01
1 package com.MavenRedis;
2
3 import redis.clients.jedis.Jedis;
4 import redis.clients.jedis.ScanResult;
5 import redis.clients.jedis.Tuple;
6
7 import java.util.List;
8
9 /**
10 * @author XuFanQi
11 * @site XUFANQI
12 * @company xxx
13 * @create 2019-10-13 10:57
14 */
15 public class Demo {
16 public static void main(String[] args) {
17 Jedis Jedis = new Jedis("192.168.183.128", 6379);
18 Jedis.auth("xiaoqi123");
19 System.out.println(Jedis.ping());
20
21 //选择数据库
22 String select = Jedis.select(0);
23 // Jedis.del("booklist");
24 //Strng 字符串
25 //set
26 //Jedis.set("name","zs");
27 //get获取
28 System.out.println(Jedis.get("booklist"));
29 //type获取类型
30 //System.out.println(Jedis.type("name"));
31 //del删除
32 //Jedis.del("name");
33 //expire设置超时时间
34 //Jedis.expire("name",10);
35
36 //hash(哈希)
37
38 // Jedis.hset("student","name","ls");
39 // Jedis.hset("student","age","13");
40 // Jedis.hset("student","addres","cx");
41
42 // System.out.println(Jedis.hget("student", "name"));
43
44 //list列表
45 // Jedis.lpush("names","zs","ls","ww");
46 // Long names = Jedis.llen("names");
47 // for (int i=0;i<names;i++){
48 //从左到右
49 // System.out.println(Jedis.lpop("names"));
50 //从左到右
51 //System.out.println(Jedis.rpop("names"));
52 // }
53
54
55 //set集合
56 // Jedis.sadd("username","admin","zs","ls");
57 // ScanResult<String> username = Jedis.sscan("username", 0);
58 // List<String> result = username.getResult();
59 // for (String s : result) {
60 // System.out.println(s);
61 // }
62
63 //zset有序集合
64 // Jedis.zadd("sex",70,"男");
65 // Jedis.zadd("sex",80,"女");
66 // Jedis.zadd("sex",90,"不男不女");
67 // ScanResult<Tuple> zscan = Jedis.zscan("sex", 0);
68 // List<Tuple> result = zscan.getResult();
69 // for (Tuple tuple : result) {
70 // System.out.println("sex="+tuple.getElement()+",score="+tuple.getScore());
71 //
72 // }
73
74
75 }
76 }
增删改中的使用
DemoServerlet.java
1 import redis.clients.jedis.Jedis;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.annotation.WebServlet;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import java.io.IOException;
9
10 @WebServlet("/list")
11 public class DemoServerlet extends HttpServlet {
12 @Override
13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
14 doPost(req,resp);
15 }
16
17 @Override
18 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19 Jedis jedis = new Jedis("192.168.183.128", 6379);
20 jedis.auth("xiaoqi123");
21 String booklist=jedis.get("bookList");
22 if(booklist==null || "".equals(booklist)){
23 //模拟实际项目开发需求,在项目中运用redis
24 //查询数据库
25 String mysqldata="data";
26 //将mysqldata数据源转成json数组串
27 jedis.set("booklist",mysqldata);
28 booklist = jedis.get("booklist");
29 req.setAttribute("mag","走了数据库数据");
30 req.setAttribute("booklist",booklist);
31 req.getRequestDispatcher("/booklist.jsp").forward(req,resp);
32 }else{
33 req.setAttribute("mag","直接从redis里面拿了数据");
34 req.setAttribute("booklist",booklist);
35 req.getRequestDispatcher("/bookList.jsp").forward(req,resp);
36 }
37 }
38
39 }