- 依赖
springDataRedis是对jedis的封装,jedis依赖也要加
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
- 配置属性值
redis.host=192.168.188.128
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
- 创建redisTemplate对象
<!-- 配置Jedis的连接池-->
<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
<!-- 最大空闲数-->
<property name="maxIdle" value="${redis.maxIdle}"/>
<!-- 最大等待时间-->
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!-- 配置Jedis的连接工厂-->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" id="jedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
<property name="database" value="${redis.database}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<!-- 声明RedisTemplate-->
<bean class="org.springframework.data.redis.core.RedisTemplate" id="redisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
4.使用redisTemplate对象操作redis
key - value类型
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue(){
redisTemplate.boundValueOps("name").set("youjiuye");
}
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
@Test
public void deleteValue(){
redisTemplate.delete("name");
}
}
key - set
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue(){
redisTemplate.boundSetOps("nameset").add("张三");
redisTemplate.boundSetOps("nameset").add("李四");
redisTemplate.boundSetOps("nameset").add("王五");
}
@Test
public void getValue(){
Set set = redisTemplate.boundSetOps("nameset").members();
System.out.println(set);
}
@Test
public void deleteValue(){
redisTemplate.boundSetOps("nameset").remove("李四");
}
@Test
public void deleteAll(){
redisTemplate.delete("nameset");
}
}
key - list
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue1() {
redisTemplate.boundListOps("namelist1").leftPush("a");
redisTemplate.boundListOps("namelist1").leftPush("b");
redisTemplate.boundListOps("namelist1").leftPush("c");
redisTemplate.boundListOps("namelist1").leftPush("d");
}
@Test
public void getValue1() {
List list = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}
@Test
public void setValue2() {
redisTemplate.boundListOps("namelist2").rightPush("1");
redisTemplate.boundListOps("namelist2").rightPush("2");
redisTemplate.boundListOps("namelist2").rightPush("3");
redisTemplate.boundListOps("namelist2").rightPush("4");
}
@Test
public void getValue2() {
List list = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}
@Test
public void getValueByIndex() {
String str = (String) redisTemplate.boundListOps("namelist1").index(2);
System.out.println(str);
}
@Test
public void deleteValueByIndex(){
redisTemplate.boundListOps("namelist2").remove(1,"3");
}
}
key - hash
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue() {
redisTemplate.boundHashOps("namehash").put("1", "张三");
redisTemplate.boundHashOps("namehash").put("2", "李四");
redisTemplate.boundHashOps("namehash").put("3", "王五");
redisTemplate.boundHashOps("namehash").put("4", "赵六");
}
@Test
public void getKeys() {
Set set = redisTemplate.boundHashOps("namehash").keys();
System.out.println(set);
}
@Test
public void getValues() {
List list = redisTemplate.boundHashOps("namehash").values();
System.out.println(list);
}
@Test
public void getValueByKey() {
String str = (String) redisTemplate.boundHashOps("namehash").get("3");
System.out.println(str);
}
@Test
public void deleteValueByKey(){
redisTemplate.boundHashOps("namehash").delete("3");
}
}
redisTemplate操作总结
redisTemplate.boundXXXOps()
key - value
redisTemplate.boundValueOps(“redisKey”).set(“redisValue”);
redisTemplate.boundValueOps("redisKey ").get();
key – set
redisTemplate.boundSetOps("redisKey ").add(“set1Value”);
redisTemplate.boundSetOps("redisKey ").members();
key – list
redisTemplate.boundListOps(“namelist1”).rightPush(“刘备”);
redisTemplate.boundListOps(“namelist1”).range(0, 10);
key – hashmap
redisTemplate.boundHashOps(“redisKey”).get(“mapKey”);
redisTemplate.boundHashOps(“redisKey”).put(“mapKey”,mapVlue);