我如何在 Spring JPA 中使用动态变量

How can i use dynamic variable in Spring JPA

@Repository
@RequiredArgsConstructor
public class RouletteRecRepository {

    private final EntityManager em;

    public RouletteRecDto findOne(RouletteRecCriteriaDto request) {

        TypedQuery<RouletteRecDto> query = em.createQuery(
                "select new opgg.weba.JamPick.dto.RouletteRecDto(i.id, i.headerImage)" +
                        " from IndieApp i join i.genres g where g.description in :request group by i.id", RouletteRecDto.class)
                .setParameter("request", request);

        RouletteRecDto result = query.getSingleResult();

        return result;
    }

}
@Data
public class RouletteRecCriteriaDto {
    private List<String> genreList = new ArrayList<>();
}
RouletteRecCriteriaDto request = new RouletteRecCriteriaDto();
request.setGenreList(List.of("RPG, ACTION"));

RouletteRecDto rouletteApp = rouletteRecRepository.findOne(request);

我将请求值初始化为字符串列表。 我想在流派列中搜索包含“RPG”或“ACTION”的数据。 但是,如果我使用 :request,它就不起作用。 (如果我改用 g.description in ('RPG', 'ACTION'),它会起作用) 我如何在 JPQL 中使用动态值? (变量的值或变量的数量可以改变。)

如果您只想在查询中传递字符串列表,请将对象 RouletteRecCriteriaDto 替换为 List<String>

List<String> list = reqeust.getGenreList();
TypedQuery<RouletteRecDto> query = em.createQuery(
            "select new opgg.weba.JamPick.dto.RouletteRecDto(i.id, i.headerImage)" +
                    " from IndieApp i join i.genres g where g.description in :request group by i.id", RouletteRecDto.class)
            .setParameter("request", list);

TypedQuery<RouletteRecDto> query = em.createQuery(
                "select new opgg.weba.JamPick.dto.RouletteRecDto(i.id, i.headerImage)" +
                        " from IndieApp i join i.genres g where g.description in :request group by i.id", RouletteRecDto.class)
                .setParameter("request", request.getGenreList());