Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Data JPA如何使用@repository传递变量_Java_Mysql_Spring Data Jpa - Fatal编程技术网

Java Spring Data JPA如何使用@repository传递变量

Java Spring Data JPA如何使用@repository传递变量,java,mysql,spring-data-jpa,Java,Mysql,Spring Data Jpa,谢谢大家!我找到了解决办法: @Repository public interface BookRepository extends JpaRepository<Book, Integer>{ @Query(value = "select * from Book where find_in_set(:market,market)", nativeQuery = true) public List<Book> findBooksByMarcke

谢谢大家!我找到了解决办法:

@Repository
public interface BookRepository extends JpaRepository<Book, Integer>{       
    @Query(value = "select * from Book where find_in_set(:market,market)", nativeQuery = true)
    public List<Book> findBooksByMarcket(@Param("market") String market);
}
@存储库
公共接口BookRepository扩展了JpaRepository{
@查询(value=“select*from Book where find_in_set(:market,market)”,nativeQuery=true)
公共列表findBooksByMarcket(@Param(“市场”)字符串市场);
}
原始问题

我使用@Query注释通过使用JPA查询语言创建查询,并将这些查询直接绑定到我的存储库接口的方法

我的数据库创建正确,我成功创建了一些查询,但以下查询除外:

@Repository
public interface BookRepository extends JpaRepository<Book, Integer>{       
    @Query("select b from Book b where find_in_set(:market,b.market)")
    public List<Book> findBooksByMarcket(@Param("market") String market);
}
@存储库
公共接口BookRepository扩展了JpaRepository{
@查询(“从书籍b中选择b,在书籍b中查找书籍集(:market,b.market)”)
公共列表findBooksByMarcket(@Param(“市场”)字符串市场);
}
通过MySql检查时,使用find_in_set函数可以得到正确的结果。但我无法用java传递变量。我在网上搜索过,但找不到正确的格式


请帮忙,谢谢你们

如果您有一个自定义MySQL函数,并且希望在JPA存储库中使用它,请查看 使用CriteriaBuilder还有另一种方法(我在JPA规范中使用了这种机制):
搜索关键词:自定义db函数、JPA规范、CriteriaBuilder、Criteria

快速解决方案是将JPQL查询转换为本机查询(通过将
nativeQuery
标志设置为true):

@Query(value=“select*from Book b,其中find_in_set(:market,b.market)”,nativeQuery=true)
公共列表findBooksByMarcket(@Param(“市场”)字符串市场);
试试这个

@Repository
public interface BookRepository extends JpaRepository<Book, Integer>{       
    @Query("select b from Book b where find_in_set(?1,b.market)")
    public List<Book> findBooksByMarcket(String market);
}
@存储库
公共接口BookRepository扩展了JpaRepository{
@查询(“从图书b中选择b,在图书集中查找图书(?1,b.market)”)
公共列表findBooksByMarcket(字符串市场);
}

在JPQL中没有find_in_set这样的东西。JPQL和SQL是两种不同的语言。顺便问一下,为什么不能使用like或=呢?有什么具体的原因吗?如果下面的任何答案解决了您的问题,请将其标记为已接受。这是另一个问题:令牌“从书b中选择b,在书b中查找_在_集中(:market,b.market)”,无效的MemberValuePairs…我认为您缺少“值”,任何异常都会出现
@Repository
public interface BookRepository extends JpaRepository<Book, Integer>{       
    @Query("select b from Book b where find_in_set(?1,b.market)")
    public List<Book> findBooksByMarcket(String market);
}