[英]PostgreSQL query works on pgAdmin but not in Spring Boot
The query SELECT * FROM books WHERE (isbn || ' ' || author || ' ' || name) ILIKE '%el%'
returns查询
SELECT * FROM books WHERE (isbn || ' ' || author || ' ' || name) ILIKE '%el%'
返回
from the full db when executed in pgAdmin (PostgreSQL tool)在 pgAdmin(PostgreSQL 工具)中执行时从完整数据库
But the same query doesn't seem to work when I try to run it in Spring Boot, it returns an empty list.但是当我尝试在 Spring Boot 中运行它时,相同的查询似乎不起作用,它返回一个空列表。 I do not know if my query is PostgreSQL specific or if it's native SQL, if it isn't native SQL then I guess it is reasonable that it does not work seeing as I have
nativeQuery = true
which if I have understood things correctly means that native SQL is expected.我不知道我的查询是特定于 PostgreSQL 还是本机 SQL,如果不是本机
nativeQuery = true
,那么我猜它是合理的预计原生 SQL。 Without it, the application does not even run.没有它,应用程序甚至无法运行。 If the case is as described, how do I specify that I want to use PostgreSQL in the query?
如果情况如前所述,如何在查询中指定我要使用 PostgreSQL?
BookController.java
@GetMapping("/search")
public String search(@RequestParam(value = "keyword") String keyword, Model model) {
List<Book> books = bookService.search(keyword);
model.addAttribute("books", books);
return "books";
}
BookService.java
public List<Book> search(String keyword) {
return bookRepository.search(keyword);
}
BookRepository.java
@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
List<Book> findAll();
@Query(value = "SELECT * FROM books WHERE (isbn || ' ' || author || ' ' || name) ILIKE '%?%'", nativeQuery = true)
List<Book> search(String keyword);
}
Books.html
the value I am entering is el
. Books.html
我输入的值是el
。
<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" th:action="@{books/search}" method="get">
<input th:name="keyword" type="search" class="form-control form-control-dark" placeholder="Search..." aria-label="Search">
</form>
The parameter ?
参数
?
may not be injected properly in the query.可能无法在查询中正确注入。 Change
'%?%'
to '%' ||? || '%'
将
'%?%'
更改为'%' ||? || '%'
'%' ||? || '%'
'%' ||? || '%'
as in: '%' ||? || '%'
如:
@Query(value = "SELECT * FROM books WHERE (isbn || ' ' || author || ' ' || name) ILIKE '%' || ? || '%'", nativeQuery = true)
try尝试
@Query(value = "SELECT * FROM books WHERE (isbn || ' ' || author || ' ' || name) ILIKE %?1%", nativeQuery = true)
List<Offers> search(String keyword);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.