简体   繁体   中英

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. 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:

@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. 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.

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

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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