使用maven实现redis与idea的连接问题

时间:2022-02-24 23:39:27

spring配置文件  加入  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- spring data redis -->
   <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
       <property name="usePool" value="true"></property>
       <property name="hostName" value="${redis.host}" />
       <property name="port" value="${redis.port}" />
       <!--  <property name="password" value=""/> -->
       <property name="timeout" value="${redis.timeout}" />
       <property name="database" value="${redis.default.db}"></property>
       <constructor-arg   ref="jedisPoolConfig" />
   </bean>
 
 
   <!-- 连接池参数配置 -->
   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
       <property name="maxTotal" value="${redis.maxActive}" />
       <property name="maxIdle" value="${redis.maxIdle}" />
       <property name="maxWaitMillis" value="${redis.maxWait}" />
   </bean>
 
   <!-- 配置redis模板,方便存取数据 -->
   <!-- <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">-->
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
       <property name="connectionFactory" ref="jedisConnectionFactory" />
   </bean>

pom.xml文件需要下载jar包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.5.1</version>
    </dependency>

 建一个测试包在src下

使用maven实现redis与idea的连接问题

 实现一些简单的功能: Set  list    hash   sortedset   时间   操作字符串等

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
import java.util.List;
import java.util.Map;
import java.util.Set;
 
public class JedisTest {
//   操作字符串
    @Test
    public void test1() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(1);
        jedis.set("userName", "张三三");
        System.out.println(jedis.get("userName"));
        jedis.close();
    }
//操作时间
    @Test
    public void test2() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(2);
        jedis.setex("Name", 15, "wsf");
        System.out.println(jedis.get("Name"));
        jedis.close();
    }
//操作list
    @Test
    public void test3() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(2);
        jedis.lpush("myList", "g", "h", "i");
        jedis.rpush("myList", "j", "k", "l");
        List<String> list = jedis.lrange("myList", 0, -1);
        for (String j : list) {
            System.out.println(j);
        }
        jedis.close();
    }
 
//操作set集合
    @Test
    public void test4() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(3);
        jedis.sadd("mySet", "zs", "李四", "王麻子", "zz");
        Set<String> set = jedis.smembers("mySet");
        for (String s : set) {
            System.out.println(s);
        }
        jedis.close();
    }
 
//操作hash类型
    @Test
    public void test5() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(4);
        jedis.hset("myhash","userName","zs");
        jedis.hset("myhash","passWord","123123");
        Map<String, String> map = jedis.hgetAll("myhash");
 
        Set<String> keys = map.keySet();
        for (String key:keys){
 
            System.out.println("key:"+key+"\tvalue:"+map.get(key));
        }
     jedis.close();
    }
 
    //操作有序集合类型sortedset
    @Test
    public void test6() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
         jedis.select(4);
         jedis.zadd("Mysort",50,"张三");
        jedis.zadd("Mysort",70,"李四");
        jedis.zadd("Mysort",30,"王五");
        jedis.zadd("Mysort",100,"ssss");
        jedis.zadd("Mysort",60,"dddd");
        jedis.zadd("Mysort",10,"aaa");
        jedis.zadd("Mysort",5,"adadw");
        Set<String>  set= jedis.zrange("Mysort",0,-1);
 
 
        for (String s:set){
 
            System.out.println(s);
        }
 
    }
    @Test
    public void test7() {
        JedisPoolConfig config = new JedisPoolConfig();
         config.setMaxTotal(50);
         config.setMinIdle(10);
        JedisPool jedisPool= new JedisPool(config,"127.0.0.1",6379);
      Jedis jedis=  jedisPool.getResource();
        jedis.set("zzzz","阿斯顿");
        System.out.println(jedis.get("xxoo"));
        jedis.close();
 
    }
 
@Test
    public  void test8(){
    Jedis jedis = new Jedis("127.0.0.1", 6379);
    jedis.select(4);
    jedis.set("userName", "武康康");
    System.out.println(jedis.get("userName"));
    jedis.close();
}
 
}

到此这篇关于使用MAVEN实现redis与idea的连接问题的文章就介绍到这了,更多相关redis连接idea内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/best_p1/article/details/118910819