redis与idea的连接用MAVEN实现

Posted 池鱼i_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis与idea的连接用MAVEN实现相关的知识,希望对你有一定的参考价值。

spring配置文件  加入  

    <!-- 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包


    <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下

 实现一些简单的功能:

Set  list    hash   sortedset   时间   操作字符串等

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();



以上是关于redis与idea的连接用MAVEN实现的主要内容,如果未能解决你的问题,请参考以下文章

redis与idea的连接用MAVEN实现

使用idea,springboot,springsession,redis实现分布式微服务的session 共享

IDEA用Maven连接MySQL的jdbc驱动,并操作数据库

idea中怎么整合springspringmvcmybatis

idea没有配置redis链接为啥还能访问到数据

webstorm、sublime、IDEA、Maven、Git有啥区别?