Redis-使用Java代码操作Redis
Java访问redis
首先在pom.xml添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
简单操作
实例化一个jedis
Jedis jedis = new Jedis("192.168.203.128",6379);
jedis.auth("123456");
System.out.println(jedis.ping());
操作字符串 添加键值
jedis.set("aaa","zs");//编辑
System.out.println(jedis.get("aaa"));//读取
在客户服务端就会出现对应值
操作哈希
jedis.hset("userl", "uname","ls");
jedis.hset("userl", "sex","男");
System.out.println(jedis.hgetAll("userl"));
System.out.println(jedis.hget("userl", "uname"));//指定
操作列表
jedis.lpush("hobby","a","b","c","d","e","f","g");
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.rpop("hobby"));
demo案例
测试
package com.chen;
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嘟嘟
*/
@WebServlet("/getData")
public class DemoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost