Java Spring JPA@Query-尝试对多个SQL参数使用1个参数;获取有关SQL语法的错误消息

Java Spring JPA@Query-尝试对多个SQL参数使用1个参数;获取有关SQL语法的错误消息,java,sql,spring,jpa,spring-data-jpa,Java,Sql,Spring,Jpa,Spring Data Jpa,我正在使用SpringJPA中的@Query注释编写一个自定义查询。当只对1或2个小字符串执行此操作时,我获得了成功-但是,我正在尝试创建一个名为args的参数,该参数可能具有多个约束/参数 例如,args参数将作为标题传递,如“%iphone%”标题%20like%20%27%25iphone%25%27 addArgs是附加约束,如标题“%12%”和%20title%20like%20%27%2512%25%27 下面是使用System.out.println打印到控制台的查询-当我将其复

我正在使用SpringJPA中的@Query注释编写一个自定义查询。当只对1或2个小字符串执行此操作时,我获得了成功-但是,我正在尝试创建一个名为args的参数,该参数可能具有多个约束/参数

例如,args参数将作为标题传递,如“%iphone%”标题%20like%20%27%25iphone%25%27

addArgs是附加约束,如标题“%12%”和%20title%20like%20%27%2512%25%27

下面是使用System.out.println打印到控制台的查询-当我将其复制并粘贴到MYSQL中时,它工作正常

select product_id as Id, 666 as session_id, price as secondary, title as returned_data  from products where title like '%iphone%' and title like '%12%' order by title asc;
当我将其粘贴到MySQL中测试查询时,它会按预期返回&看起来不错。但是,我在运行Java应用程序时出错

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''products' where title like 'title like \'%iphone%\'' 'and title like \'%12%\'' ' at line 1
我是新手,所以这可能是一种笨拙的方式。有人能帮助我了解出了什么问题,或者为什么这不能按预期工作?提前谢谢

编辑/更新:

需要的是使用EntityManager动态构建查询。
查询中存在语法错误

要创建完全动态的查询,需要创建自定义存储库,并在其中构建动态查询,然后执行它

以下是最低限度的代码:

@Repository
public interface CustomRepository {
    public List<ReturnObj> customQueryMethod(Long param);
}

@Repository
public class CustomRepositoryImpl implements CustomRepository {
    
    @Autowired
    private EntityManager entityManager;
    
    public List<ReturnObj> customQueryMethod(Long param)  {
        // build sql query here
        Query query = entityManager.createNativeQuery(sql, Tuple.class);
        List<Tuple> data = query.getResultList();
        // do operations on Tuple data and return
    }
}

@Repository
public interface MainRepository extends JpaRepository<MainEntity, Long>, CustomRepository {}

现在,无论您想在何处使用此自定义存储库方法,只需自动连接Main存储库,并在那里调用customQueryMethod。

抱歉,我已经开始尝试调试一段时间了。我实际尝试的是这样的:value=select:id作为id,:session作为sessionId,:secondary作为secondary,:primary作为returnedData from:table其中:args:addargs order by:order;,where:args封装了后面的所有内容,where。。。我会调整我上面的问题来反映这一点。哦,好的。您希望动态生成查询。那么我想这是不可能的。您需要创建自定义存储库,并在其中动态构建查询,然后使用EntityManager执行查询。让我相应地更新我的答案。检查我更新的答案。Pass:addArgs param为null(如果不需要),它将在查询中处理。JPA所做的是在启动应用程序时生成并缓存所有查询。它提高了性能。在执行过程中,它只需要将参数传递给查询。因此,您不能创建这样的动态查询。创建动态查询的唯一方法是使用自定义存储库和EntityManager。
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''products' where title like 'title like \'%iphone%\'' 'and title like \'%12%\'' ' at line 1
@Repository
public interface CustomRepository {
    public List<ReturnObj> customQueryMethod(Long param);
}

@Repository
public class CustomRepositoryImpl implements CustomRepository {
    
    @Autowired
    private EntityManager entityManager;
    
    public List<ReturnObj> customQueryMethod(Long param)  {
        // build sql query here
        Query query = entityManager.createNativeQuery(sql, Tuple.class);
        List<Tuple> data = query.getResultList();
        // do operations on Tuple data and return
    }
}

@Repository
public interface MainRepository extends JpaRepository<MainEntity, Long>, CustomRepository {}