springboot集成redis步骤
1.创建springboot项目
2.配置连接
3.测试
创建springboot项目
创建以一个Maven项目
创建之后查看pom.xml配置文件,可以看到
pom文件里面导入了 data-redis 的依赖,那我们就可以在知道,springboot集成redis操作redis数据库是通过这个依赖完成的,点进这个依赖,他的底层
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency> 


配置连接
 
#配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379 
配置依赖在application.properties文件里面配置
需要配置的东西在 RedisProperties.class,之里面现在是默认的本地配置如果有需要修改其他主机的配置那就可以在配置文件里进行修改


在application.properties里还可以配置连接池,之前使用的是jedis,现在建议使用的是lettuce,因为在RedisAutoConfiguration类中的redisTemplate方法中引入一个了一个接口RedisConnectionFactory的参数,如果可以看到源码的话点进去可以看到RedisConnectionFactory的实现类,它的实现类有两种一个是JedisConnectionFactory实现类这里很多的方法是不生效的,那就是说不可以用,但是在LettuceConnectionFactory实现类里是生效的所以这里推荐使用连接池lettuce



测试
1.在测试类导入RedisTemplate数据库驱动能通过redisTemplate对象调用方法来操作redis
@Autowired
private RedisTemplate redisTemplate; 
 
 
使用细节
序列化问题
Redis 对象都需要序列化(否则会报错)

默认的 RedisTemplate<Object, Object> 两个泛型都是object类型后面使用需要强制转换,我们平时常使用的是 RedisTemplate<String, Object>所以后面我们可以去替换这个默认的RedisTemplate。
由于我们在Redis 中尝试用的是String类型所以Redis单独提出来了一个bean
/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.springframework.boot.autoconfigure.data.redis;
 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
 
/**
 * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis support.
 *
 * @author Dave Syer
 * @author Andy Wilkinson
 * @author Christian Dupuis
 * @author Christoph Strobl
 * @author Phillip Webb
 * @author Eddú Meléndez
 * @author Stephane Nicoll
 * @author Marco Aust
 * @author Mark Paluch
 * @since 1.0.0
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
 
	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}
 
	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		return new StringRedisTemplate(redisConnectionFactory);
	}
 
} 
                


















