您好,登录后才能下订单哦!
这篇文章主要为大家展示了“怎么解决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是别名
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 的时候,通常我们只需要继承 JpaRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句
但是在使用 Spring Data JPA 的时候,通常我们只需要继承 JpaRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句
@Query(value = "自定义sql语句", nativeQuery = true) List<Long> findFriendsByUserId(Long userId);
如上,只需在查询语句后边加上nativeQuery = true 就可以了
以上是“怎么解决springdataJPA对原生sql支持的问题”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。