52.11. Spring Data Redis - Repository Examples
52.11.1. @EnableRedisRepositories 启动 Redis 仓库
package api.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
@Configuration
@EnableRedisRepositories
public class CachingConfigurer {
}
package api.domain;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Reference;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
@RedisHash("persons")
public class Person {
public enum Gender {
FEMALE, MALE
}
@Id
private String id;
@Indexed
private String firstname;
@Indexed
private String lastname;
private Gender gender;
private Address address;
@Reference
private List<Person> children;
public Person() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<Person> getChildren() {
return children;
}
public void setChildren(List<Person> children) {
this.children = children;
}
@Override
public String toString() {
return "Person [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", gender=" + gender + ", address=" + address + ", children=" + children + "]";
}
}
package api.domain;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.index.GeoIndexed;
import org.springframework.data.redis.core.index.Indexed;
public class Address {
private @Indexed String city;
private String country;
private @GeoIndexed Point location;
public Address(String city, String country, Point location) {
this.city = city;
this.country = country;
this.location = location;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
}
package api.repository;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.geo.Circle;
import org.springframework.data.repository.CrudRepository;
import api.domain.Person;
public interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findByLastname(String lastname);
Page<Person> findPersonByLastname(String lastname, Pageable page);
List<Person> findByFirstnameAndLastname(String firstname, String lastname);
List<Person> findByFirstnameOrLastname(String firstname, String lastname);
List<Person> findByAddress_City(String city);
List<Person> findByAddress_LocationWithin(Circle circle);
}
package api.restful;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import api.domain.Person;
import api.domain.Address;
import api.repository.PersonRepository;
@RestController
@RequestMapping("/test")
public class TestRestController {
private static final Logger logger = LoggerFactory.getLogger(TestRestController.class);
@Autowired
private PersonRepository personRepository;
public TestRestController() {
}
@GetMapping("/redis")
public Person redis() {
Person children = new Person();
children.setFirstname("Lisa");
children.setLastname("Chen");
children.setGender(Person.Gender.FEMALE);
Person person = new Person();
person.setFirstname("Neo");
person.setLastname("Chen");
person.setGender(Person.Gender.MALE);
// List<Person> childrens = new ArrayList<Person>();
person.setChildren(Arrays.asList(children));
Point point = new Point(Double.valueOf("28.352734"), Double.valueOf("32.807382"));
Address address = new Address("Shenzhen", "China", point);
person.setAddress(address);
personRepository.save(person);
return person;
}
}