2013-04-05 87 views
0

我是初學者。如何在春季創建我的queru

我嘗試創建一個自定義查找器。並使用「repository jpa」命令。 這是我的倉庫:

@RooJpaRepository(domainType = Speaker.class) 
public interface SpeakerRepository { 
    @Query("select u from Speaker u where username = :un") 
    public List<Speaker> findllAllSpeakersNamed(@Param("un") String lastname); 
    } 

服務:

public class SpeakerServiceImpl implements SpeakerService { 
@Autowired 
SpeakerRepository speakerRepository; 
public List<Speaker> findllAllSpeakersNamed(String lastName) { 
    return speakerRepository.findllAllSpeakersNamed(lastName); 
} 
} 

和控制器:

@RequestMapping("/findASpeaker/**") 
@Controller 
public class FindASpeaker { 

    @Autowired 
    SpeakerServiceImpl speakerServiceImpl; 

    @RequestMapping(method = RequestMethod.POST, value = "{id}") 
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) { 
    } 


    @RequestMapping 
    public String index(@RequestParam("lastname")String lastname) { 
     String Lastname=lastname; 
     ModelMap modle=new ModelMap(); 


     List<Speaker> list = speakerServiceImpl.findllAllSpeakersNamed(Lastname); 
     modle.addAttribute("speakers",list); 

     return "findASpeaker/index"; 
    } 

} 

這可不行,,,,

回答

0

你的資料庫是一個接口。 Spring Data應該根據方法的名稱自動檢測查詢,例如「List findUsersByNameDesc(String name);」將自動創建一個方法來查詢給定名稱的所有用戶。 @Query只是告訴它不要猜測查詢,而是使用你提供的那個。一個例子可以是found here