我正尝试使用Spring Data和Redis进行CRUD操作,但主要需要在Redis中存储自增键。
我已经尝试了SpringData与Redis的简单CRUD操作,但没有自增键功能。
我该如何实现这个?
我正尝试使用Spring Data和Redis进行CRUD操作,但主要需要在Redis中存储自增键。
我已经尝试了SpringData与Redis的简单CRUD操作,但没有自增键功能。
我该如何实现这个?
org.springframework.data.annotation.Id
,以自动生成值,并在其类上添加@RedisHash
注释。@RedisHash("persons")
public class Person {
@Id String id;
String firstname;
String lastname;
Address address;
}
现在,如果想要一个负责存储和检索的组件,您需要定义一个仓库接口。
public interface PersonRepository extends CrudRepository<Person, String> {
}
@Configuration
@EnableRedisRepositories
public class ApplicationConfig {
@Bean
public RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
return template;
}
}
@Autowired PersonRepository repo;
public void basicCrudOperations() {
Person rand = new Person("rand", "al'thor");
rand.setAddress(new Address("emond's field", "andor"));
repo.save(rand); //1
repo.findOne(rand.getId()); //2
repo.count(); //3
repo.delete(rand); //4
}
参考文献:http://docs.spring.io/spring-data/redis/docs/current/reference/html/