亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Java代碼操作Redis過程詳解

 更新時間:2019年10月14日 09:38:49   作者:江東maker  
這篇文章主要介紹了基于Java代碼操作Redis過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Jedis簡介

實際開發(fā)中,我們需要用Redis的連接工具連接Redis然后操作Redis,

對于主流語言,Redis都提供了對應的客戶端;

提供了很多客戶端 官方推薦的是Jedis 托管地址:https://github.com/xetorthio/jedis

要使用redis首先得下載pom依賴

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

連接redis

Jedis jedis = new Jedis("192.168.234.131",6379);
jedis.auth("123456");
//jedis.close(); //使用完關(guān)閉連
System.out.println(jedis.ping());

使用Jedis連接池之后,在每次用完連接對象后一定要記得把連接歸還給連接池。Jedis對close方法進行了改造,如果是連接池中的連接對象,調(diào)用Close方法將會是把連接對象返回到對象池,若不是則關(guān)閉連接。

操作Redis常用的三種數(shù)據(jù)類型 ,還有兩種不常用的就不介紹了

package com.wp;
import redis.clients.jedis.Jedis;
/**
* @author 小李飛刀
* @site www.xiaomage.com
* @company zhuojing
* @create 2019-10-13 10:43
*/
public class javaDome {
	public static void main(String[] args) {
		//鏈接操作
		Jedis jedis = new Jedis("192.168.234.131",6379);
		jedis.auth("123456");
		System.out.println(jedis.ping());
		//操作字符串
		jedis.set("aaa","張三");
		System.out.println(jedis.get("aaa"));
		//對哈希進行操作
		jedis.hset("user1","uname","李四");
		jedis.hset("user1","sex","男");
		System.out.println(jedis.hgetAll("user1"));
		System.out.println(jedis.hget("user1", "uname"));
		//對list進行操作
		jedis.lpush("hobby","a","b","c","d","e","f");
		System.out.println(jedis.lpop("hobby"));
		System.out.println(jedis.lpop("hobby"));
		System.out.println(jedis.rpop("hobby"));
	}
}

項目中的應用演示例子

查詢中使用redis的邏輯

如圖所示:

bookservlet.java

代碼如下:

package web;
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 小李飛刀
* @site www.xiaomage.com
* @company zhuojing
* @create 2019-10-13 10:43
*/
@WebServlet("/wp")
public class Bookservlet 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.234.131",6379);
		jedis.auth("123456");
		String booklist = jedis.get("booklist");
		if(booklist==null || "".equals(booklist)){
			//模擬實際項目開發(fā)需求,在項目中運用redis
			//查詢數(shù)據(jù)庫
			String mysqldata="data";
			//將mysqldata數(shù)據(jù)源轉(zhuǎn)成json數(shù)組串
			jedis.set("booklist",mysqldata);
			booklist = jedis.get("booklist");
			req.setAttribute("mag","走了數(shù)據(jù)庫數(shù)據(jù)");
			req.setAttribute("booklist",booklist);
			req.getRequestDispatcher("/booklist.jsp").forward(req,resp);
		} else{
			req.setAttribute("mag","直接從redis里面拿了數(shù)據(jù)");
			req.setAttribute("booklist",booklist);
			req.getRequestDispatcher("/index.jsp").forward(req,resp);
		}
	}
}

index.jsp

<html>

<body>
	<h2>Hello World!</h2>
	<%-- Created by IntelliJ IDEA. User: machenike
	Date: 2019/10/13 Time: 11:53 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>
					${mag}:${booklist}
				</body>

				</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論