解决springdataJPA对原生sql支持的问题

 更新时间:2021年06月11日 10:06:23   作者:WangXiaoYuuuuu  
这篇文章主要介绍了解决springdataJPA对原生sql支持的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springdataJPA对原生sql支持问题

在项目中用到的是springdataJPA连接数据库进行操作,但是JPA中的hql语句不能够满足业务要求,因而需要用到原生sql

但是有一个问题:

  @Query(value = "SELECT ppd.* FROM  zt_productionplandetails AS ppd \n" +
            "       \tLEFT JOIN zt_salesplan  sp ON sp.id=ppd.salesPlan_id \n" +
            "         \tLEFT JOIN zt_employee  e ON e.id=ppd.employeeId\n" +
            "          \tWHERE ppd.enabled = TRUE \n" +
            "          \tAND ppd.`status`=1 \n" +
            "\tAND IF(:clientName !='', sp.clientName LIKE %:clientName%, 1 = 1 )\n" +
            "           \tAND IF( :productName !='', sp.productName LIKE %:productName%, 1 = 1 )\n" +
            "            \tAND IF( :empName != '', e.name LIKE %:empName%, 1 = 1 )\n" +
            "            \tAND IF( :startDate != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) >=:startDate, 1 = 1 )\n" +
            "            \tAND IF( :endDate != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) <=:endDate, 1 = 1 )"
            ,nativeQuery = true)
    Page<ProductionPlanDetails> findNewPlan(@Param("productName") String productName, @Param("empName") String empName, @Param("endDate") String endDate, @Param("startDate") String startDate, @Param("clientName") String clientName, Pageable pageable);

在用这个sql的时候,会报错

java.sql.SQLSyntaxErrorException: Unknown column 'ppd' in 'field list'

意思就是找不到ppd的字段,可是这里明显可以看出ppd是别名

后查看项目启动查询数据调用的sql为:

select count(ppd) FROM  zt_productionplandetails AS ppd 
        LEFT JOIN zt_salesplan  sp ON sp.id=ppd.salesPlan_id 
          LEFT JOIN zt_employee  e ON e.id=ppd.employeeId
           WHERE ppd.enabled = TRUE 
           AND ppd.`status`=1 
 AND IF(? !='', sp.clientName LIKE ?, 1 = 1 )
            AND IF( ? !='', sp.productName LIKE ?, 1 = 1 )
             AND IF( ? != '', e.name LIKE ?, 1 = 1 )
             AND IF( ? != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) >=?, 1 = 1 )
             AND IF( ? != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) <=?, 1 = 1 )

这里就发现了问题所在,在项目执行sql的时候,查询的是count(ppd),查询文档得知默认情况下,jpa会在执行查询sql的时候会加上count()

所以做以下修改:

@Query(value = "SELECT ppd.* FROM  zt_productionplandetails AS ppd \n" +
            "       \tLEFT JOIN zt_salesplan  sp ON sp.id=ppd.salesPlan_id \n" +
            "         \tLEFT JOIN zt_employee  e ON e.id=ppd.employeeId\n" +
            "          \tWHERE ppd.enabled = TRUE \n" +
            "          \tAND ppd.`status`=1 \n" +
            "\tAND IF(:clientName !='', sp.clientName LIKE %:clientName%, 1 = 1 )\n" +
            "           \tAND IF( :productName !='', sp.productName LIKE %:productName%, 1 = 1 )\n" +
            "            \tAND IF( :empName != '', e.name LIKE %:empName%, 1 = 1 )\n" +
            "            \tAND IF( :startDate != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) >=:startDate, 1 = 1 )\n" +
            "            \tAND IF( :endDate != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) <=:endDate, 1 = 1 )",
            countQuery = "SELECT count(*) FROM  zt_productionplandetails AS ppd \n" +
            "            \tLEFT JOIN zt_salesplan  sp ON sp.id=ppd.salesPlan_id \n" +
            "            \tLEFT JOIN zt_employee  e ON e.id=ppd.employeeId\n" +
            "            \tWHERE ppd.enabled = TRUE \n" +
            "            \tAND ppd.`status`=1 \n" +
            "\tAND IF(:clientName !='', sp.clientName LIKE %:clientName%, 1 = 1 )\n" +
            "            \tAND IF( :productName !='', sp.productName LIKE %:productName%, 1 = 1 )\n" +
            "            \tAND IF( :empName != '', e.name LIKE %:empName%, 1 = 1 )\n" +
            "            \tAND IF( :startDate != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) >=:startDate, 1 = 1 )\n" +
            "            \tAND IF( :endDate != '', DATE_FORMAT(ppd.createDate, '%Y-%m-%d %k:%i:%s' ) <=:endDate, 1 = 1 )"
            ,nativeQuery = true)

加上countQuery参数

解决问题~

Spring Data JPA 写原生sql语句

在使用 Spring Data JPA 的时候,通常我们只需要继承 JpaRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句

但是在使用 Spring Data JPA 的时候,通常我们只需要继承 JpaRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句

@Query(value = "自定义sql语句", nativeQuery = true)
List<Long> findFriendsByUserId(Long userId);

如上,只需在查询语句后边加上nativeQuery = true 就可以了

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

最新评论