有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java BaseRepository查询实体列表返回对象数组列表

我正在使用SpringJPA框架实现Hibernate。 我在BaseRepository中声明了一个查询,如下所示:

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryDslPredicateExecutor<T> {
    @Query("select e.id,e.name from #{#entityName} e where e.state=1 and e.name like CONCAT(:name,'%')")
    List<T> findIdAndNameByNameStartingWith(@Param("name") String name);
}

并在控制器中使用

@RequestMapping("/list/byName")
    public HttpApiResponse findAllByNameLike(@RequestParam(defaultValue = "") String name) {
        List<Platform> platforms = platformRepository.findIdAndNameByNameStartingWith(name);
        return HttpApiResponse.doSuccess(platforms);
    }

但是当我调试时,我发现findIdAndNameByNameStartingWith()返回列表像[['hotel',1],'travel',2]]而不是List<Platform>。谁能给我一些建议吗?谢谢


共 (1) 个答案

  1. # 1 楼答案

    它会回复你的要求

    select e.id,e.name from #{#entityName} e where e.state=1 and e.name like CONCAT(:name,'%')

    试着换成

    select e from #{#entityName} e where e.state=1 and e.name like CONCAT(:name,'%')