首页 > 编程语言 > 使用maven实现redis与idea的连接问题
2021
11-01

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

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

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

编程技巧