两种:spring整合redis,springboot整合redis
环境:jdk1.6;spring+springmvc+ibatis+maven;redis3.2;
pom.xml:
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.7.1.RELEASEversion>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.1version>
dependency>
不使用maven可以自行下载jar包,复制到项目中:jar下载1,jar下载2
#redis配置
spring.redis.host=使用的redis主机地址,本地redis即127.0.0.1
spring.redis.port=redis服务的端口,默认为6379
spring.redis.password=redis服务密码
spring.redis.timeout=redis客户端超过该空闲时间会断开
更多详细配置参考:https://www.cnblogs.com/metu/p/9609604.html
文件名可任取,后缀为properties
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.propertiesvalue>
list>
property>
bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="usePool" value="true" />
<property name="hostName" value="${spring.redis.host}" />
<property name="port" value="${spring.redis.port}" />
<property name="timeout" value="${spring.redis.timeout}" />
<property name="password" value="${spring.redis.password}" />
<property name="poolConfig" ref="jedisPoolConfig" />
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringSerializer">property>
<property name="hashKeySerializer" ref="stringSerializer">property>
<property name="hashValueSerializer" ref="stringSerializer">property>
bean>
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer">bean>
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("redistest.do")
public Set test() {
Set<ZSetOperations.TypedTuple<String>> listset = new LinkedHashSet();
ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<String>("test1",1);
ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<String>("test2",2);
listset.add(objectTypedTuple1);
listset.add(objectTypedTuple2);
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.opsForZSet().add(GaotimeConstants.DEFAULT_MENU_KEY, listset);//新增有序hash
Set<HashMap> listresult = redisTemplate.opsForZSet().range(INFOPRODUCT_MENU_KEY, 0, redisTemplate.opsForZSet().size(INFOPRODUCT_MENU_KEY)); //查询
redisTemplate.opsForZSet().removeRange(INFOPRODUCT_MENU_KEY, 0, redisTemplate.opsForZSet().size(INFOPRODUCT_MENU_KEY)); //删除
System.out.println(redisTemplate.opsForZSet().size(INFOPRODUCT_MENU_KEY)); //打印长度
此外,还有更多操作的api,也就不一 一列举了。
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.11.RELEASEversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
dependencies>
在application.yml或者application.properties中配置:
#redis
#redis数据库索引(默认为0)
spring.redis.database=0
spring.redis.host=172.18.1.44
spring.redis.port=6379
spring.redis.password=!ZykUGb8
#连接池最大连接数(使用负数表示没有限制)默认8
spring.redis.jedis.pool.max-active=10
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=6000
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
如上述第4部分一致。