我有一个自定义搜索页面,我可以在其中输入最多 6 个搜索选项。此选项与
AND
结合使用,因此通过使用所有过滤器,我有 6 个 where 子句等等。
两个问题:
有没有办法用 spring data jdbc 编写这个动态 where 查询?
我如何访问底层
DataSource
(或者可能是JdbcTemplate
)来直接触发此查询?
例如:
this.jdbcTemplate.query = query // the dynamic query with 6 or less where clauses;
List<Result> results = this.jdbcTemplate.run();
谢谢!
编辑
有一个
JdbcAggregateTemplate
和 <T> Iterable<T> findAll(Query query, Class<T> domainType);
查询。这是正确的地方吗?
与其他帖子所述相反,我无法自动装配
EntityManager
bean。
您有多种选择:
您可以从填充了一些条目的实体创建一个
Example
:
Person person = new Person();
person.setFirstname("Dave");
Example<Person> example = Example.of(person);
然后将其与您的存储库一起使用,这将扩展
QueryByExampleExecutor
:
public interface QueryByExampleExecutor<T> {
<S extends T> S findOne(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example);
// … more functionality omitted.
}
您可以微调示例的使用方式:
Person person = new Person();
person.setFirstname("Dave");
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("lastname")
.withIncludeNullValues()
.withStringMatcher(StringMatcher.ENDING);
Example<Person> example = Example.of(person, matcher);
JdbcAggregateTemplate
它具有接受
Query
的半公开功能。
它没有正确记录。查看源代码以了解如何使用它。
JdbcTemplate
您必须手动构建 SQL
EntityManger
它是一个 JPA 制品,Spring Data JDBC 与 JPA 无关。