我正在使用org.springframework.data.jpa.domain.Specification和JpaSpecificationExecutor来轻松创建带有Java条件的查询,但现在我需要调用一个返回整数值的MySQL DB函数。
问题是目前还不清楚如何传递此函数的参数,因为我没有使用TypedQuery:
// cb here is CriteriaBuilder
ParameterExpression param = cb.parameter(String.class);
Predicate predicate = cb.greaterThan(cb.function("A_FUNCTION",
Integer.class, param), 0);
Specification spec = cb.and(predicate);
// query is executed like this
return (int) repositoryThatExtendsJpaSpecificationExecutor.count(test);
来自here的示例没有帮助。
答案 0 :(得分:3)
我认为您真正需要的是一个可以使用CriteriaBuilder.literal
创建的文字。一个完整的例子看起来像
cmake_minimum_required(VERSION 2.8)
# Project
project(005-executable-with-shared-library)
# Directories
set(example_BIN_DIR bin)
set(example_INC_DIR include)
set(example_LIB_DIR lib)
set(example_SRC_DIR src)
# Library files
set(library_SOURCES ${example_SRC_DIR}/library.cpp)
set(library_HEADERS ${example_INC_DIR}/library.h)
set(executable_SOURCES ${example_SRC_DIR}/main.cpp)
# Setting RPATH
# See https://cmake.org/Wiki/CMake_RPATH_handling
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${example_LIB_DIR})
# Add library to project
add_library(library SHARED ${library_SOURCES})
# Include directories
target_include_directories(library PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/${example_INC_DIR})
# Add executable to project
add_executable(executable ${executable_SOURCES})
# Linking
target_link_libraries(executable PRIVATE library)
# Install
install(TARGETS executable DESTINATION ${example_BIN_DIR})
install(TARGETS library DESTINATION ${example_LIB_DIR})
install(FILES ${library_HEADERS} DESTINATION ${example_INC_DIR})
如果值不是来自您的应用程序,您可以使用(m)任何functions returning an Expression
。