如何在 Spring Data JDBC 中使用许多可选的查询参数?

问题描述

我们想使用存储在 POJO 中的许多可选搜索条件来查询表

const sess = await mongoose.startSession();
sess.startTransaction();
await first.save({ session: sess }); 
await second.save({ session: sess });
await sess.commitTransaction();

MyBatis 超级简单:https://mybatis.org/mybatis-3/dynamic-sql.html

public class TemperatureMeterCriteria {

    private String state;
    private String title;
    private LocalDate date;
    private Year year;
    private Integer month;
    private Integer week;
    private LocalDate startDate;
    private LocalDate endDate;
    private DateQuery dateQuery;
    private AlertLevel alertLevel;
    ...

}

但是,如何用 Spring Data JDBC 做同样的事情?

https://docs.spring.io/spring-data/jdbc/docs/current/reference/html

解决方法

目前唯一的选择是创建自定义方法并在内部使用第三方库,如 Querydsl、jOOQ 或 MyBatis。

通过 JdbcAggregateTemplate 提供方法的工作正在进行中,该方法将接受可用于此类目的的 Condition。第一步(仅删除)将作为 https://github.com/spring-projects/spring-data-jdbc/issues/771

的副作用到达