[英]How to reuse native query code for different tables and views?
我想知道这是否可能。 如果我们有这样的情况:
@Query(nativeQuery = true,
value = "SELECT TA.* " +
"FROM TABLE_A TA " +
"WHERE " +
"(LOWER(NAME) LIKE NVL(CONCAT('%', CONCAT(LOWER(:#{#filter.name}), '%')), NAME) " +
"OR LOWER(CODE) LIKE NVL(CONCAT('%', CONCAT(LOWER(:#{#filter.code}), '%')), CODE)) " +
"AND (TO_CHAR(USER_WORKFLOW_ID) IN (:#{#filter.userWorkFlowIds}) OR COALESCE(:#{#filter.userWorkFlowIds}, NULL) IS NULL) " +
"ORDER BY " +
" CASE " +
" WHEN :#{#filter.orderByColumn.index} = 4 AND :#{#filter.orderDirection.index} = -1 THEN VERSION " +
" END DESC, " +
" CASE " +
" WHEN :#{#filter.orderByColumn.index} = 10 AND :#{#filter.orderDirection.index} = -1 THEN PROGRESS " +
" END DESC " +
"OFFSET LOWER(:#{#filter.pageSize} * :#{#filter.pageIndex}) ROWS FETCH NEXT LOWER(:#{#filter.pageSize}) ROWS ONLY ")
List<TableAItems> findByFilter(@Param(value = "filter") TAFilter filter);
和
@Query(nativeQuery = true,
value = "SELECT TB.* " +
"FROM TABLE_B TB " +
"WHERE " +
"(LOWER(NAME) LIKE NVL(CONCAT('%', CONCAT(LOWER(:#{#filter.name}), '%')), NAME) " +
"OR LOWER(CODE) LIKE NVL(CONCAT('%', CONCAT(LOWER(:#{#filter.code}), '%')), CODE)) " +
"AND (TO_CHAR(USER_WORKFLOW_ID) IN (:#{#filter.userWorkFlowIds}) OR COALESCE(:#{#filter.userWorkFlowIds}, NULL) IS NULL) " +
"ORDER BY " +
" CASE " +
" WHEN :#{#filter.orderByColumn.index} = 4 AND :#{#filter.orderDirection.index} = -1 THEN VERSION " +
" END DESC, " +
" CASE " +
" WHEN :#{#filter.orderByColumn.index} = 10 AND :#{#filter.orderDirection.index} = -1 THEN PROGRESS " +
" END DESC " +
"OFFSET LOWER(:#{#filter.pageSize} * :#{#filter.pageIndex}) ROWS FETCH NEXT LOWER(:#{#filter.pageSize}) ROWS ONLY ")
List<TableBItems> findByFilter(@Param(value = "filter") TBFilter filter);
where 我们有两个完全相同的 where 子句,有没有办法创建一些东西来重用它?
当然
只需将此部分放在一个常量(Java 中的最终 static)中,然后在注释中使用它。
例子
@Query(nativeQuery = true,
value = "SELECT TB.* " +
"FROM TABLE_B TB " +
MyClass.WHERE +
"ORDER BY " +
" CASE " +
" WHEN :#{#filter.orderByColumn.index} = 4 AND :#{#filter.orderDirection.index} = -1 THEN VERSION " +
" END DESC, " +
" CASE " +
" WHEN :#{#filter.orderByColumn.index} = 10 AND :#{#filter.orderDirection.index} = -1 THEN PROGRESS " +
" END DESC " +
"OFFSET LOWER(:#{#filter.pageSize} * :#{#filter.pageIndex}) ROWS FETCH NEXT LOWER(:#{#filter.pageSize}) ROWS ONLY ")
List<TableBItems> findByFilter(@Param(value = "filter") TBFilter filter);
然后是常数
public final static String WHERE = "WHERE " +
"(LOWER(NAME) LIKE NVL(CONCAT('%', CONCAT(LOWER(:#{#filter.name}), '%')), NAME) " +
"OR LOWER(CODE) LIKE NVL(CONCAT('%', CONCAT(LOWER(:#{#filter.code}), '%')), CODE)) " +
"AND (TO_CHAR(USER_WORKFLOW_ID) IN (:#{#filter.userWorkFlowIds}) OR COALESCE(:#{#filter.userWorkFlowIds}, NULL) IS NULL) " +
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.