pom.xml文件需要的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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.1 . 0 .RELEASE</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.11 </version> <scope>test</scope> </dependency> |
Spring配置文件需要的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!-- 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> |
在resources中创建application.properties文件
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 |
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\: //127.0.0.1:3306/mybatis01?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root
#定义初始连接数 jdbc.initialSize= 0 #定义最大连接数 jdbc.maxActive= 20 #定义最大空闲 jdbc.maxIdle= 20 #定义最小空闲 jdbc.minIdle= 1 #定义最长等待时间 jdbc.maxWait= 60000
dbcp.initialSize= 15 dbcp.maxActive= 5000 dbcp.maxIdle= 0 dbcp.maxWait= 900000 dbcp.defaultAutoCommit= true dbcp.removeAbandoned= true dbcp.removeAbandonedTimeout= 30 dbcp.whenExhaustedAction= 1 dbcp.validationQuery=select 1 dbcp.testOnBorrow=fasle dbcp.testOnReturn= false
#redis的服务器地址 redis.host= 127.0 . 0.1 #redis的服务端口 redis.port= 6379 #密码 redis.pass=root #链接数据库 redis. default .db= 0 #客户端超时时间单位是毫秒 redis.timeout= 100000 #最大连接数 redis.maxActive= 300 #最大空闲数 redis.maxIdle= 100 #最大建立连接等待时间 redis.maxWait= 1000 #指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 #DBSync.testOnBorrow= true redis.clientName=requirepass |
redis几种类型的测试(String、list、hash、set、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 |
package cn.hp;
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; import java.util.stream.Stream;
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( 1 ); jedis.setex( "code" , 20 , "778899" ); System.out.println(jedis.get( "code" )); jedis.close(); }
/** * */ @Test public void test3(){ Jedis jedis = new Jedis( "127.0.0.1" , 6379 ); jedis.lpush( "mylist" , "b" , "c" , "d" ); jedis.rpush( "mylist" , "e" , "f" , "g" ); List<String> jlist = jedis.lrange( "mylist" , 0 ,- 1 ); for (String j:jlist){ System.out.println(j); } jedis.close(); }
/** * 操作set集合 */ @Test public void test4(){ Jedis jedis = new Jedis( "127.0.0.1" , 6379 ); jedis.sadd( "myset" , "zs" , "ls" , "ww" , "zl" ); 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.hset( "myhash" , "userName" , "zs" ); jedis.hset( "myhash" , "pass" , "123456" ); Map<String ,String>map = jedis.hgetAll( "myhash" ); Set<String> keys = map.keySet(); for (String k:keys){ String value = map.get(k); System.out.println( "k" +k+ "value:" +value); } jedis.close(); }
/** * 排序 */ @Test public void test6(){ Jedis jedis = new Jedis( "127.0.0.1" , 6379 ); jedis.zadd( "mysort" , 90 , "zs" ); jedis.zadd( "mysort" , 80 , "ls" ); jedis.zadd( "mysort" , 70 , "ww" ); Set<String> set=jedis.zrange( "mysort" , 0 ,- 1 ); for (String s:set){ System.out.println(s); } jedis.close(); }
/** * jedis 连接池使用 */ @Test public void test7(){ JedisPoolConfig config= new JedisPoolConfig(); //创建一个配置对象 config.setMaxTotal( 50 ); config.setMaxIdle( 10 ); JedisPool jedisPool = new JedisPool(config, "127.0.0.1" , 6379 ); //获取连接池 //从连接池拿到一个jedis连接 Jedis jedis = jedisPool.getResource(); jedis.set( "aabb" , "123" ); jedis.close(); //归还连接 }
} |
到此这篇关于基于 idea Maven中的 redis配置 使用的文章就介绍到这了,更多相关idea redis配置使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/wbcra/article/details/118911783
查看更多关于基于idea Maven中的redis配置使用详解的详细内容...