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

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 keys = jedis.keys("*");
        Iterator 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 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 keys = jedis.keys("*");
        Iterator 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 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);
    }
}


    4.0.0

    com.atguigu
    boot3-01
    1.0-SNAPSHOT


    
        org.springframework.boot
        spring-boot-starter-parent
        3.1.4
    
    

        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            junit
            junit
            test
        
        
            redis.clients
            jedis
            4.4.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


    4.0.0

    com.atguigu
    boot3-01
    1.0-SNAPSHOT


    
        org.springframework.boot
        spring-boot-starter-parent
        3.1.4
    
    

        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            junit
            junit
            test
        
        
            redis.clients
            jedis
            4.4.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
[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"

你可能感兴趣的:(#,Redis,JAVA,#,Spring-Boot框架,redis,java-ee,java,spring,boot,spring,后端,缓存)