1.实体
package tju.SpringBootRedisStudy3.domain;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
@RedisHash("yuangong1")
public class Person {
String id;
@Indexed
String firstname;
String lastname;
Address address;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
package tju.SpringBootRedisStudy3.domain;
public class Address {
String city;
String country;
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;
}
}
2.DAO
package tju.SpringBootRedisStudy3.dao;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import tju.SpringBootRedisStudy3.domain.Person;
public interface PersonRepository2 extends CrudRepository<Person,String> {
Iterable<Person> findByFirstname(String firstname);
}
- Service
package tju.SpringBootRedisStudy3.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.stereotype.Service;
import tju.SpringBootRedisStudy3.dao.PersonRepository;
import tju.SpringBootRedisStudy3.dao.PersonRepository2;
import tju.SpringBootRedisStudy3.domain.Person;
@Service
public class PersonService2 {
@Autowired
PersonRepository2 personRepository2;
public Iterable<Person> findPeople2(String firstname) {
return personRepository2.findByFirstname(firstname);
}
}
- Controller
package tju.SpringBootRedisStudy3.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tju.SpringBootRedisStudy3.domain.Address;
import tju.SpringBootRedisStudy3.domain.Person;
import tju.SpringBootRedisStudy3.service.PersonService;
import tju.SpringBootRedisStudy3.service.PersonService2;
@RestController
public class PersonController {
@Autowired
private PersonService personService;
@Autowired
private PersonService2 personService2;
@RequestMapping("/save/{firstname}/{lastname}")
public String savePerson(@PathVariable("firstname")String firstname,@PathVariable("lastname")String lastname)
{
Address address=new Address();
address.setCity("beijing");
address.setCountry("china");
Person p=new Person();
p.setAddress(address);
p.setFirstname(firstname);
p.setLastname(lastname);
personService.savePerson(p);
return "ok";
}
@RequestMapping("/find/{firstname}")
public String find(@PathVariable("firstname")String firstname)
{
Iterable<Person> it=personService2.findPeople2(firstname);
if(it!=null) {
for(Person p: it) {
System.out.println(p.getFirstname()+","+p.getLastname());
}
}
else {
System.out.println("no item!!");
}
return "ok";
}
}