我正在尝试通过 java 连接到 Redis,我有以下配置,@SpringBootApplication@Configuration@ComponentScan(basePackages = "com.infy.redisDemo")public class RedisDemoApplication { @Bean public LettuceConnectionFactory getConnectionFactory(){ LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory("localhost",6379); return lettuceConnectionFactory; } @Bean public RedisTemplate<String,Object> getRedisTemplate(){ RedisTemplate<String, Object> redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(getConnectionFactory()); return redisTemplate; } public static void main(String[] args) { SpringApplication.run(RedisDemoApplication.class, args); }}我的回购课程:@Repositorypublic class UserRepoImpl implements UserRepository { private RedisTemplate<String, Object> redisTemplate; private HashOperations hashOperations; public final String key = "USER"; public UserRepoImpl(RedisTemplate redisTemplate, HashOperations hashOperations) { this.redisTemplate = redisTemplate; this.hashOperations = redisTemplate.opsForHash(); } @Override public void save(User user) { hashOperations.put(key,user.getId(),user); } @Override public void update(User user) { hashOperations.put(key,user.getId(),user); } @Override public Map findAll() { return hashOperations.entries(key); }}public interface UserRepository { void save(User user); void update(User user); Map findAll();}
1 回答
万千封印
TA贡献1891条经验 获得超3个赞
您不需要HashOperations hashOperations在构造函数中作为参数传递UserRepoImpl。相反,您可以从构造函数中删除此参数。
public UserRepoImpl(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
}
添加回答
举报
0/150
提交
取消