Redis: spring data redis 方法名查询,必须给属性加索引

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);
}



  1. 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);
	  }
	  
	  
	}
  1. 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";
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值