这不仅仅是一个简单的问题,而且我的英语没有我想要的那么好……我会尽力而为。
我使用 java 8,Mybatis 3.4.6 而不是 Postgres 9.6,我需要进行自定义动态查询。
在我的 mapper.java 类中,我创建了一个与myBatis SQL Builder 类一起使用的方法
@SelectProvider(type = PreIngestManager.class, method = "selectPreIngestsSQLBuilder")
@Results({ @Result(property = "id", column = "id"), @Result(property = "inputPath", column = "input_path"),
@Result(property = "idCategoriaDocumentale", column = "id_categoria_documentale"), @Result(property = "idCliente", column = "id_cliente"),
@Result(property = "outputSipPath", column = "output_sip_path"), @Result(property = "esito", column = "esito"),
@Result(property = "stato", column = "stato"), @Result(property = "pathRdp", column = "path_rdp"),
@Result(property = "dataInizio", column = "data_inizio"), @Result(property = "dataFine", column = "data_fine") })
List<PreIngest> selectPreIngestsByFilters(@Param("idCatDoc") Long idCatDoc, @Param("nomePacchetto") String nomePacchetto,
@Param("dataInizioInferiore") Date dataInizioInferiore, @Param("dataInizioSuperiore") Date dataInizioSuperiore,
@Param("statiPreIngest") String statiPreIngest);
我已经指定了 @SelectProvider 注释、类和指向的方法,在示例中是 PreIngestManager.class 和 selectPreIngestsSQLBuilder 方法。
这是方法
public String selectPreIngestsSQLBuilder(Map<String, Object> params) {
return new SQL() {
{
SELECT("*");
FROM("pre_ingest");
WHERE("id_categoria_documentale = #{idCatDoc}");
if (params.get("nomePacchetto") != null)
WHERE("input_path like '%' || #{nomePacchetto}");
if (params.get("dataInizioInferiore") != null) {
if (params.get("dataInizioSuperiore") != null) {
WHERE("data_inizio between #{dataInizioInferiore} and #{dataInizioSuperiore}");
} else {
WHERE("data_inizio >= #{dataInizioInferiore}");
}
} else {
if (params.get("dataInizioSuperiore") != null) {
WHERE("data_inizio <= #{dataInizioSuperiore}");
}
}
if (params.get("statiPreIngest") != null)
WHERE("stato in (#{statiPreIngest})");
ORDER_BY("id ASC");
}
}.toString();
}
这些是我的问题:
我必须指定 @Results 注释和每个 @Result ,还是可以使用 java 模型类?我试过 @ResultMap(value = { "mycompany.model.PreIngest" }) 但它没有用。
最重要的是,如文档所述,使用 SQL 构建器,您可以访问将它们作为最终对象的方法参数
// With conditionals (note the final parameters, required for the anonymous inner class to access them)
public String selectPersonLike(final String id, final String firstName,
final String lastName) {
return new SQL() {{
SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME");
FROM("PERSON P");
if (id != null) {
WHERE("P.ID like #{id}");
}
if (firstName != null) {
WHERE("P.FIRST_NAME like #{firstName}");
}
if (lastName != null) {
WHERE("P.LAST_NAME like #{lastName}");
}
ORDER_BY("P.LAST_NAME");
}}.toString();
}
但是,如果我将它们放在我的方法中,我将无法访问它们。我需要从方法声明中删除@Param 吗?是否需要在没有 @SelectProvider 的情况下调用 SQLBuilder ?我在混合解决方案吗?
据我研究,现在我看到了 3 种方法来进行动态查询或自定义 where 条件。
- 使用 MyBatisGenerator 库并将 where 条件作为搜索条件与 SelectByExample 方法结合使用。(当查询很简单时我使用它)
- 直接编写 SQL 查询,使用 if、choose、statements 等修改 XML 映射器文件,如下所述
- 使用带有 @SelectProvider 注释的SQL Builder 类。
你知道什么时候更喜欢 2° 方法而不是 3° 方法吗?为什么在 3° 方法文档中我找不到如何使用它?写了如何创建自定义查询,但没有写如何启动它们。
非常感谢您的时间和建议。