我正在创建一个本地查询,该查询从多个表中获取有限的字段。这些字段使用sql结果集映射映射到投影类构造函数。我正在尝试通过传递分页参数进行排序。排序属性是通过URL请求参数排序决定的。
下面是代码段
<named-native-query
name="EmployeeDet.findEmployeeAddressDTOUsingPageableUsingNativeQuery"
result-set-mapping="CustomEmplMappingUsingXml">
<query><![CDATA[select e.id,e.empName,e.dateOfBirth as dob,e.gender,e.email,address.typename
as addtype,address.addressdet,et.employeeTypeName as employeeType
from employeedet as e,Address as address,employeetype as et
where address.empid=e.id and e.employeeTypeId=et.employeeTypeId
group by e.id,e.empName,e.dateOfBirth,e.gender,e.email,address.typename,
address.addressdet,et.employeeTypeName ORDER BY ?#{#pageable}]]>
</query>
</named-native-query>
<sql-result-set-mapping
name="CustomEmplMappingUsingXml">
<constructor-result
target-class="com.springdatajpa.config.dto.EmployeeAddressDtoProjectionClass">
<column name="id" class="java.lang.Integer" />
<column name="empName" class="java.lang.String" />
<column name="email" class="java.lang.String" />
<column name="dob" class="java.lang.String" />
<column name="gender" class="java.lang.String" />
<column name="addtype" class="java.lang.String" />
<column name="addressdet" class="java.lang.String" />
<column name="employeeType" class="java.lang.String" />
</constructor-result>
</sql-result-set-mapping>
上述查询是从存储库层调用的,其方法名称类似于命名的本机查询名称
@Query(nativeQuery=true)
Page<EmployeeAddressDtoProjectionClass> findEmployeeAddressDTOUsingPageableUsingNativeQuery(Pageable pageable);
提到的可分页参数通过url传递,值为
?page=0&size=3&sort=empId,DESC
由于返回值为Page类型,因此我正在使用另一个查询来对行数进行计数
<named-native-query
name="EmployeeDet.findEmployeeAddressDTOUsingPageableUsingNativeQuery.count">
<query><![CDATA[select count(*) from employeedet as e,Address as address,employeetype et
where address.empid=e.id and et.employeeTypeId=e.employeeTypeId
group by e.id,e.empName,e.dateOfBirth,e.gender,e.email,address.typename,address.addressdet,et.employeeTypeName
]]></query>
</named-native-query>
上面的查询运行时出现错误
org.springframework.dao.InvalidDataAccessResourceUsageException:预期的位置参数计数:1,实际检测到0
我尝试了堆栈溢出中不同链接中提到的解决方案,但仍然遇到异常。