RedisTemplate 是redis 提供的一个操作redis数据库的工具类
1。引入包
org.springframework.context-3.1.0.RELEASE。jar
2.总结一下开发当中学用的几种格式
1) redistemplate.opsForValue()
2) redistemplate.opsForList()
3) redistemplate.opsForSet()
4) redistemplate.opsForHash()
分别为存储 String 、list、set 、map、treeset
开发中用法 :
1)存储String 以 key vlaue的形式
redistemplate.opsForValue.set(key, value);
redistemplate.opsForValue.get(key);
批量插入
public Map<String, Object> map= new HashMap<String,Object>();
redisTemplate.opsForValue().multiSet(map);
注:object 也可换实体类。。。
取一个KEY下面的对像
redisTemplate.opsForValue().get(key);
批量取list 中存在KEY的值
List<String> list= new ArrayList<>();
List<Object> orgMappingRelationList = redisTemplate.opsForValue().multiGet(list);
2)存储map 时的用法
Map<Long, Object> value = new HashMap<>();
redisTemplate.opsForHash().putAll(KEY, value );
删除key下面对像对应的lists中的值
List<String> lists= new ArrayList<>();
redisTemplate.opsForHash.delete(key,lists); //参数类型是 Collection 所以传入 set list 都是可以的
redisTemplate.opsForHash.delete(key); //没错这个方法是一个多态的方法。可以传入String 删除一整个KEY值
redisTemplate.opsForHash().get(Key, hashKey);//取key下面 hashKey对应的值
Map<Object, Object> map= redisTemplate.opsForHash().entries(key);//取一个key下面所有键值
3)存储LIST
List<Object> list= new ArrayList<>();
redisTemplate.opsForList().leftPushAll(key, list);//插入
List<Object> lists= redisTemplate.opsForList().range(key, 0, -1);//取值 0 是起始 -1 是结束值 (-1是取全部)
4)存储set
redisTemplate.opsForSet().add(KEY, value);//向key下面的SET列表中插入一下值
Set<Object> set = userRoleSetRedisTemplate.opsForSet().members(KEY);//取值
redisTemplate.opsForSet().remove( KEY, setkey);
3.集成Spring配置
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory"/>
<bean id="redisSentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg index="0" value=" --ip地址--" />
<constructor-arg index="1" value="--端口--" />
</bean>
</set>
</property>
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="200" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:pool-config-ref="poolConfig">
<constructor-arg ref="redisSentinelConfiguration"/>
</bean>