Using java to operate redis

HairLossException 2022-01-26 11:28:34 阅读数:636

using java operate redis

1、 establish maven java engineering

 Insert picture description here

2、 Add dependency

stay <dependencies> Add under label jedis Dependence and fastjson rely on

<!--jedis rely on -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!--fastjson rely on -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>

3、 establish Jedis object

public class AppTest {

@Test
public void test(){

/* host:ip Address port: Port number */
Jedis jedis = new Jedis("192.168.140.129",6379);
//ping redis The server Returns if the connection is successful pong
String returnMessage = jedis.ping();
System.out.println(returnMessage);
// Save the data 
jedis.set("name","zhangshan");
jedis.set("age","20");
// Take the data 
String name = jedis.get("name");
String age = jedis.get("age");
System.out.println("name="+name+"\nage="+age);
}
}

4、 start-up redis service

redis-server redis6379.conf & // start-up redis service
systemctl stop firewalld // Turn off firewall

 Insert picture description here

5、 test

 Insert picture description here

copyright:author[HairLossException],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261128327232.html