c语言sscanf函数的用法是什么
262
2023-01-08
解决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
在用这个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 =bmhpAyHrMt "SELECT count(*) FROM zt_productionplandetails AS ppd \n" +
" \tLEFT JOhttp://IN 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 的时候,通常我们只需要继承 JpabmhpAyHrMtRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句
@Query(value = "自定义sql语句", nativeQuery = true)
List
如上,只需在查询语句后边加上nativeQuery = true 就可以了
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~