以下使用的是配置文件整合(单机Redis)
整合的版本,spring5.2.7 + Redis客户端Jedis3.1.0 + Redis6.0.6
1、创建新的maven空项目.
略
目录结构
2、导入依赖.
<properties>
<spring.version>5.2.7.RELEASE</spring.version>
</properties>
<dependencies>
<!-- springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- lombok 需要导入插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<!-- redis -->
<!-- spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<!-- redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
3、创建redis.properties
.
文件内容如下
redis.host=ip地址
redis.port=6379 # 默认端口
redis.password=你的Redis设置的密码 如果没有就不必设置
redis.timeout=2000
redis.pool.maxTotal=100
redis.pool.maxIdle=20
redis.pool.maxWaitMills=2000
redis.pool.testOnBorrow=true
4、编写 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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="redis.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最小维持连接数 -->
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<!-- 最大可用连接数 -->
<property name="maxTotal" value="${redis.pool.maxTotal}"/>
<!-- 最大等待连接数 -->
<property name="maxWaitMillis" value="${redis.pool.maxWaitMills}"/>
<!-- 可用连接 -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="jedisPoolConfig"/>
<property name="hostName" value="${redis.host}"/>
<property name="password" value="${redis.password}"/>
<property name="timeout" value="${redis.timeout}"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="valueSerializer">
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="stringSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
</property>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
</beans>
5、编写实体类 User
.
需要序列化
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -6954400516630825467L;
private String name;
private String pwd;
}
6、编写测试类.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestRedis {
@Autowired
RedisTemplate<String, Object> redisTemplate;
@Test
public void test(){
ValueOperations<String, Object> str = redisTemplate.opsForValue();
str.set("spring5","redis6");
System.out.println(str.get("spring5"));
}
}
测试结果