使用java代码操作redis

使用java代码操作redis

查询中使用redis逻辑

redis在增删改查中的使用

1. Java访问redis

2.1 添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

2.2 Java连接redis

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

Demo01

package com.zl;

import redis.clients.jedis.Jedis;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-13 19:25
 *
 *
 * 讲解java代码操作redis
 * 包括string、hash、list
 *
 * 1、加载驱动
 * 2、建立连接(url、uname、pwd)
 * 3、prepatestatement
 * 4、执行sql
 * 5、处理结果
 * 6、关闭资源
 *
 *
 * redis连接步骤
 * 1、建立连接、连接后授权
 * 2、使用redis
 *
 */
public class Demo1 {
    public static void main(String[] args) {
        Jedis jedis=new Jedis("192.168.142.128",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","set","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());
//        }
        /**
         * list  left
         */
//        jedis.lpush("hobby","a","b","c","d","e","f","g");
//        System.out.println(jedis.lpop("hobby"));

    }

}

DemoServerlet

package com.zl;

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;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-13 23:08
 *
 *
 * redis 在项目中的使用
 *          查询
 *          增删改
 */
@WebServlet("/list")
public class DemoServlet 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 {
        Jedis jedis=new Jedis("192.168.142.128",6379);
        jedis.auth("123456");

        String bookList = jedis.get("bookList");
        if(null==bookList ||"".equals(bookList)){
            //查询数据库
         String mysqlData="data";
         //将mysqldata转成json数组串
         jedis.set("booList",mysqlData);
         bookList = jedis.get("bookList");
         req.setAttribute("msg","走了数据库拿数据");
         req.setAttribute("bookList","boohList");
         req.getRequestDispatcher("/bookList.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg","直接聪redis中取数据");
            req.setAttribute("bookList","boohList");
            req.getRequestDispatcher("/bookList.jsp").forward(req,resp);
        }
        }

}

bookList

注意这一段
<%@ page isELIgnored="false" %>
<%--
  Created by IntelliJ IDEA.
  User: 2018111801
  Date: 2019/10/13
  Time: 23:33
  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</title>
</head>
<body>
${msg}:${bookList}
</body>
</html>

原文地址:https://www.cnblogs.com/BAYOUA/p/11669173.html

时间: 2024-10-07 18:20:08

使用java代码操作redis的相关文章

使用Java代码操作 Redis

使用Java代码操作 RedisJedis简介实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis,对于主流语言,Redis都提供了对应的客户端:提供了很多客户端 官方推荐的是Jedis 托管地址:https://github.com/xetorthio/jedis 要使用redis首先得下载pom依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis<

java:redis(java代码操作redis)

1.redis_demo Maven  ItemMapper.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespa

Redis介绍 &amp;&amp; Java客户端操作Redis

Redis介绍 && Java客户端操作Redis 本文内容 redis介绍 redis的 shell 客户端简介 redis的 java 客户端简介 环境配置 redis 2.8.17 64bit JDK1.6 redis介绍 大多数时候,我们都将 redis 称作是内存数据库,它在运行过程中,将键值对信息存储在内存中,同时在后台以异步的形式写入本地数据库中(默认是:dump.rdb,在 redis.conf 中配置,如果需要考虑安全的持久化需求需要开启 AOF 功能,详细介绍可以查看这

java中操作redis

1 package com.redis; 2 3 import java.io.IOException; 4 import java.util.concurrent.ConcurrentHashMap; 5 6 import redis.clients.jedis.Jedis; 7 import redis.clients.jedis.Transaction; 8 /** 9 * java中操作redis 10 * 11 * @author Administrator 12 * 13 */ 14

Java代码操作zookeeper

以下为一个完整JAVA操作Zookeeper项目步骤: 1. 项目中pom.xml中添加需要的依赖jar包信息 <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <

Zookeeper客户端java代码操作

Zookeeper客户端java代码操作 上篇博客记录了shell命令操作zookeeper集群的方式,这次尝试采用java代码来操作.通过查阅API,发现并不困难. 1. 首先获得客户端与服务器的连接 //zookeeper客户端 private ZooKeeper zkCli; //连接地址 private static final String CONNECT_STRING = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; //s

Java 连接操作 Redis 出现错误

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect at redis.clients.jedis.Connection.connect(Connection.java:207) at redis.clients.jedis.BinaryClient.connect

Java代码操作HDFS(在/user/root/下面創建目錄)

1.创建HDFS目录并打成jar包 1 package Hdfs; 2 3 import java.io.IOException; 4 import java.net.URI; 5 6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.fs.FileSystem; 8 import org.apache.hadoop.fs.Path; 9 /** 10 * 创建HDFS目录 11 * @author S

Java代码操作oracle数据库(JDBC+sevrlet+jsp+html)

1, 注册页面 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"