使用java代码操作Redis

1 导入 pom.xml 依赖

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

2,Java 代码操作一下存值取值

public class demo1 {
    public static void main(String[] args) {
        Jedis Jedis = new Jedis("192.168.171.129", 6379);
        Jedis.auth("123456");
//         System.out.println(Jedis.ping());
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">选择数据库</span>
    String <span style="color: rgba(0, 0, 255, 1)">select</span> = Jedis.<span style="color: rgba(0, 0, 255, 1)">select</span>(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Strng 字符串
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">set

// Jedis.set("name","张三");
//get 获取
//System.out.println(Jedis.get("name"));
//type 获取类型
//System.out.println(Jedis.type("name"));
//del 删除
//Jedis.del("name");
//expire 设置超时时间
//Jedis.expire("name",10);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">hash(哈希)

// Jedis.hset("student","name","李四");
// Jedis.hset("student","age","13");
// Jedis.hset("student","addres","长沙");

// System.out.println(Jedis.hget("student", "name"));

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

// Jedis.lpush("names","张三","李四","王五");
// Long names = Jedis.llen("names");
// for (int i=0;i<names;i++){
//// 从左到右
// System.out.println(Jedis.lpop("names"));
//// 从左到右
// System.out.println(Jedis.rpop("names"));
// }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">set集合

// Jedis.sadd("username","admin","zs","ls");
// ScanResult<String> username = Jedis.sscan("username", 0);
// List<String> result = username.getResult();
// for (String s : result) {
// System.out.println(s);
// }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">zset有序集合

// Jedis.zadd("sex",70,"男");
// Jedis.zadd("sex",80,"女");
// Jedis.zadd("sex",90,"不男不女");
// ScanResult<Tuple> zscan = Jedis.zscan("sex", 0);
// List<Tuple> result = zscan.getResult();
// for (Tuple tuple : result) {
// System.out.println("sex="+tuple.getElement()+",score="+tuple.getScore());
//
// }

}
}

 

 

 DemoServerlet.java

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;

@WebServlet("/list")
public class DemoServerlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}

@Override
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">192.168.171.129</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">6379</span><span style="color: rgba(0, 0, 0, 1)">);
    jedis.auth(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    String booklist</span>=jedis.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bookList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(booklist==<span style="color: rgba(0, 0, 255, 1)">null</span> || <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">.equals(booklist)){
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模拟实际项目开发需求,在项目中运用redis
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询数据库</span>
        String mysqldata=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">data</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将mysqldata数据源转成json数组串</span>
        jedis.<span style="color: rgba(0, 0, 255, 1)">set</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">booklist</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,mysqldata);
        booklist </span>= jedis.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">booklist</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        req.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mag</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">走了数据库数据</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        req.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">booklist</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,booklist);
        req.getRequestDispatcher(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/booklist.jsp</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).forward(req,resp);
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
        req.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mag</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">直接从redis里面拿了数据</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        req.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">booklist</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,booklist);
        req.getRequestDispatcher(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/bookList.jsp</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).forward(req,resp);
    }
}

}

 

 bookList.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${mag}:${booklist}

</body>
</html>

 

 今天就到这里了谢谢大家!