简体   繁体   English

如何在MyBatis中将参数从@Result传递给@One

[英]How can I pass parameters to @One from @Result in MyBatis

I am working with temporal versioned data, so most of my queries require both a point in time and a group id. 我正在使用时态版本化数据,因此大多数查询都需要时间点和组ID。 How can I pass these to my nested many to one queries? 如何将这些传递给嵌套的多对一查询?

For example: 例如:

@Select("select * "
        + "from type1 "
        + "where effective_date <= #{0, typeHandler=...} "
        + "and termination_date > #{0, typeHandler=...}")
@Results({
    @Result(property = "id", column = "id"),
    @Result(property = "groupId", column = "group_id"),
    // ...
    @Result(property = "type2", column = "type2_group_id", javaType = Type2.class,
        one = @One(select = "com...mapper.Type2Mapper.getByGroupId")) // **
    // ...
})

Such that Type2Mapper has: 这样Type2Mapper具有:

@Select(...)
Type2 getByGroupId(Date date, Long groupId);

The line identified by // ** must pass both the Date passed into it along with the type2_group_id. // **标识的行必须同时传递传递给它的Date和type2_group_id。 Obviously I can rearrange things or whatever it takes to make this work, I'm just hoping I don't have to go to the point of changing my models to include a Long type2GroupId that I then populate the type2 instance after the fact. 显然,我可以重新安排事情或完成该工作所需的一切,我只是希望不必更改模型以包含Long type2GroupId,然后在事后填充type2实例。

Any thoughts? 有什么想法吗?

I could not find any way to do this, so I added support for it: https://github.com/mybatis/mybatis-3/pull/203 我找不到任何方法来执行此操作,因此我添加了对此的支持: https : //github.com/mybatis/mybatis-3/pull/203

@Result(property = "type2", column = "type2_group_id", javaType = Type2.class,
    one = @One(select = "com...mapper.Type2Mapper.getByGroupId",
            parameterProvider = @OneParameterProvider(
                    type = TemporalParameterProvider.class, method = "getParametersWithDate"
            )))

And: 和:

public class TemporalParameterProvider {
    public Object getParametersWithDate(Object value, Class<?> type, List<Object> originalParameters) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("0", originalParameters.get(0));
        parameters.put("1", value);
        return parameters;
    }
}

This could also be used to query off of calculations from the original column value, and hopefully for other use cases as well. 这也可以用于查询原始列值中的计算结果,并希望用于其他用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM