Spring-Boot框架整合Jedis实现对Redis数据库的操作------Spring-Boot框架

138 篇文章 0 订阅
57 篇文章 0 订阅
本文介绍了如何在SpringBoot项目中通过Jedis库连接Redis服务器,执行基本的数据操作(如设置、获取、列表push和range),以及设置键的过期时间。
摘要由CSDN通过智能技术生成
package com.atguigu.boot.demo;

import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class jedisDemo
{
    public static void main(String[] args) {
//        通过connection获得,通过指定Ip和端口号进行连接
        Jedis jedis = new Jedis("192.168.92.129", 6379);
//        指定访问服务器的密码
        jedis.auth("abc123");
//        获得了jedis客户端,可以像JDBC一样操作了
        System.out.println(jedis.ping());
//        获取keys *
        Set<String> keys = jedis.keys("*");
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        jedis.set("k3","Hello");
        System.out.println(jedis.get("k3"));
        System.out.println(jedis.lpush("list3", "1", "2", "3", "HEllo", "hehe"));
        List<String> list3 = jedis.lrange("list3", 0L, -1L);
        for (int i = 0; i < list3.size(); i++) {
            System.out.println(list3.get(i));
        }
        for(String s : list3){
            System.out.println(s);
        }
        long count = jedis.expire("list3", 20);
        System.out.println(count);
    }
}
package com.atguigu.boot.demo;

import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class jedisDemo
{
    public static void main(String[] args) {
//        通过connection获得,通过指定Ip和端口号进行连接
        Jedis jedis = new Jedis("192.168.92.129", 6379);
//        指定访问服务器的密码
        jedis.auth("abc123");
//        获得了jedis客户端,可以像JDBC一样操作了
        System.out.println(jedis.ping());
//        获取keys *
        Set<String> keys = jedis.keys("*");
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        jedis.set("k3","Hello");
        System.out.println(jedis.get("k3"));
        System.out.println(jedis.lpush("list3", "1", "2", "3", "HEllo", "hehe"));
        List<String> list3 = jedis.lrange("list3", 0L, -1L);
        for (int i = 0; i < list3.size(); i++) {
            System.out.println(list3.get(i));
        }
        for(String s : list3){
            System.out.println(s);
        }
        long count = jedis.expire("list3", 20);
        System.out.println(count);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>boot3-01</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    所有SpringBoot都必须继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
    </parent>
    <dependencies>
<!--        redis相关场景-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.4.3</version>
        </dependency>
    </dependencies>
<!--    SpringBoot打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>boot3-01</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    所有SpringBoot都必须继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
    </parent>
    <dependencies>
<!--        redis相关场景-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.4.3</version>
        </dependency>
    </dependencies>
<!--    SpringBoot打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@localhost ~]# redis-server /myredis/redis6379.conf 
[root@localhost ~]# redis-cli -a abc123 -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
 1) "k12"
 2) "ksas"
 3) "k10000"
 4) "email"
 5) "user:001"
 6) "list"
 7) "k3"
 8) "k4"
 9) "k2"
10) "key"
11) "k200"
12) "k11"
13) "k5"
14) "k6"
15) "k7"
16) "balance"
17) "k300"
18) "count"
19) "k1"
20) "k100"
127.0.0.1:6379> lpush list2 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> lrange list2 0 -1
1) "9"
2) "8"
3) "7"
4) "6"
5) "5"
6) "4"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> lrange list3 0 -1
 1) "hehe"
 2) "\xe5\x93\x88\xe5\x93\x88"
 3) "3"
 4) "2"
 5) "1"
 6) "hehe"
 7) "\xe5\x93\x88\xe5\x93\x88"
 8) "3"
 9) "2"
10) "1"
127.0.0.1:6379> lrange list3 0 -1
 1) "hehe"
 2) "HEllo"
 3) "3"
 4) "2"
 5) "1"
 6) "hehe"
 7) "\xe5\x93\x88\xe5\x93\x88"
 8) "3"
 9) "2"
10) "1"
11) "hehe"
12) "\xe5\x93\x88\xe5\x93\x88"
13) "3"
14) "2"
15) "1"

 [root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@localhost ~]# redis-server /myredis/redis6379.conf 
[root@localhost ~]# redis-cli -a abc123 -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
 1) "k12"
 2) "ksas"
 3) "k10000"
 4) "email"
 5) "user:001"
 6) "list"
 7) "k3"
 8) "k4"
 9) "k2"
10) "key"
11) "k200"
12) "k11"
13) "k5"
14) "k6"
15) "k7"
16) "balance"
17) "k300"
18) "count"
19) "k1"
20) "k100"
127.0.0.1:6379> lpush list2 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> lrange list2 0 -1
1) "9"
2) "8"
3) "7"
4) "6"
5) "5"
6) "4"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> lrange list3 0 -1
 1) "hehe"
 2) "\xe5\x93\x88\xe5\x93\x88"
 3) "3"
 4) "2"
 5) "1"
 6) "hehe"
 7) "\xe5\x93\x88\xe5\x93\x88"
 8) "3"
 9) "2"
10) "1"
127.0.0.1:6379> lrange list3 0 -1
 1) "hehe"
 2) "HEllo"
 3) "3"
 4) "2"
 5) "1"
 6) "hehe"
 7) "\xe5\x93\x88\xe5\x93\x88"
 8) "3"
 9) "2"
10) "1"
11) "hehe"
12) "\xe5\x93\x88\xe5\x93\x88"
13) "3"
14) "2"
15) "1"

  • 27
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值