1.导入jar或对应pom(亲测可行)
下载地址:https://mvnrepository.com/
2.编写redis-config.properties
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=6000
redis.blockWhenExhausted=true
3.编写applicationContext-redis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<context:property-placeholder location="classpath*:*.properties"/>
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- 最大空连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!-- 连接超时时是否阻塞,false时报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" />
<!-- 返回连接时,检测连接是否成功 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="JedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
p:pool-config-ref="poolConfig"/>
<!-- spring提供的模板对象 通过模板对象操作redis-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory"/>
<!--针对各种数据选择序列化方式,不序列化会导致查询和设置key异常-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
</beans>
4. 测试
/**
* @author laijiangfeng
* @date 2022/8/18 17:04
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="classpath*:applicationContext-redis.xml")
public class JunitTest {
@Resource
private RedisTemplate redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("jf","22");
Object o = redisTemplate.opsForValue().get("jf");
System.out.println(o);
redisTemplate.opsForValue().set("ljf",123);
}
}
5.序列化配置问题
<!-- spring提供的模板对象 通过模板对象操作redis-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory"/>
<!--1.针对各种数据选择序列化方式,不序列化会导致查询和设置key异常,优点:占用内存小,效率高|缺点:只能存string型,需要转化比较麻烦-->
<!-- <property name="keySerializer">-->
<!-- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />-->
<!-- </property>-->
<!-- <property name="valueSerializer">-->
<!-- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />-->
<!-- </property>-->
<!-- <property name="hashKeySerializer">-->
<!-- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />-->
<!-- </property>-->
<!-- 2.如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!,优点:存储快,可以不用考虑类型|缺点:服务端查询的值是乱码,不利于调试,占用内存大-->
<!-- <property name="keySerializer">-->
<!-- <!–对key的默认序列化器。默认值是StringSerializer–>-->
<!-- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>-->
<!-- </property>-->
<!-- <!–是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。–>-->
<!-- <property name="valueSerializer">-->
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
<!-- </property>-->
<!-- <!–存储Map时key需要的序列化配置–>-->
<!-- <property name="hashKeySerializer">-->
<!-- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>-->
<!-- </property>-->
<!-- <!–存储Map时value需要的序列化配置–>-->
<!-- <property name="hashValueSerializer">-->
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
<!-- </property>-->
<!--3.GenericJackson2JsonRedisSerializer序列化,优点:可读性较好,存储方便|缺点:占用内存较大,ssm个人选用-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<!--4.Jackson2RedisSerializer序列化,配置文件的方式不知道,会配置的可以说下,谢谢,可以通过@bean配置,boot个人选用 -->
</bean>