配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="5"></property>
<property name="maxWaitMillis" value="5000"></property>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="jedisPoolConfig"></property>
<property name="hostName" value="localhost"></property>
<property name="port" value="6379"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
</beans>
实体类的对象
package cn.itcast.pojo;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试1
package cn.itcast;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class Test01 {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testString() {
redisTemplate.opsForValue().set("name","zhangsan");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
@Test
public void testHash() {
}
@Test
public void testList() {
}
}
测试2
package cn.itcast;
import cn.itcast.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class Test02 {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testString() {
User user = (User) redisTemplate.boundValueOps("user001").get();
System.out.println(user.getName());
System.out.println(user.getAge());
}
@Test
public void testHash() {
redisTemplate.boundHashOps("users").put("name","王五");
redisTemplate.boundHashOps("users").put("age","20");
redisTemplate.boundHashOps("users").put("gender","男");
Object name = redisTemplate.boundHashOps("users").get("name");
System.out.println(name);
Map users = redisTemplate.boundHashOps("users").entries();
for (Object key : users.keySet()) {
System.out.println(key + "--" + users.get(key));
}
Set keys = redisTemplate.boundHashOps("users").keys();
System.out.println(keys);
}
@Test
public void testList() {
redisTemplate.opsForList().leftPush("list01","马蓉");
redisTemplate.opsForList().leftPush("list01","宝强");
redisTemplate.opsForList().leftPush("list01","宋吉吉");
redisTemplate.opsForList().rightPush("list01","百合");
List list = redisTemplate.opsForList().range("list01", 0, -1);
System.out.println(list);
}
@Test
public void testSet() {
}
}
Pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast</groupId>
<artifactId>12_redisTemplate</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- spring data redis实现 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
</dependencies>
</project>