基于idea Maven中的redis配置使用详解
pom.xml文件需要的内容
<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配置文件需要的内容
<!-- spring data redis --> <bean 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 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 class='org.springframework.data.redis.core.StringRedisTemplate'>--> <bean class='org.springframework.data.redis.core.RedisTemplate'><property name='connectionFactory' ref='jedisConnectionFactory' /> </bean>
在resources中创建application.properties文件
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis01?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=root #定义初始连接数jdbc.initialSize=0#定义最大连接数jdbc.maxActive=20#定义最大空闲jdbc.maxIdle=20#定义最小空闲jdbc.minIdle=1#定义最长等待时间jdbc.maxWait=60000 dbcp.initialSize=15dbcp.maxActive=5000dbcp.maxIdle=0dbcp.maxWait=900000dbcp.defaultAutoCommit=truedbcp.removeAbandoned=truedbcp.removeAbandonedTimeout=30dbcp.whenExhaustedAction=1dbcp.validationQuery=select 1dbcp.testOnBorrow=fasledbcp.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=trueredis.clientName=requirepass
redis几种类型的测试(String、list、hash、set、sortedset)
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配置使用内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: