Skip to content

Conversation

happysca
Copy link
Contributor

@happysca happysca commented Sep 25, 2022

In my use case of MyBatisCursorItemReader like below, I have to specify query parameters before the Spring Container is initialized, which works well at first.

@Configuration
public class SomeStepConfig {
    ...

    @Bean
    public Step someStep() {
        return stepBuilderFactory.get("someStep")
                .<SomeIn, SomeOut>chunk(1000)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
    MyBatisCursorItemReader reader() {
        return new MyBatisCursorItemReaderBuilder<SomeIn>()
                .sqlSessionFactory(this.sqlSessionFactory)
                .queryId("selectSelectId")
                .parameterValues(Collections.singletonMap("key", "value"))
                .build();
    }

    ...
}

After a few iterations, the query parameters needs to be determined by a preceding step, which is after Spring Container initialized, and I didn't come up with any elegant solution. So I made a copy of source code of MyBatisCursorItemReaderBuilder & MyBatisCursorItemReader in my project, and then made some changes to them as shown in this PR.

The client code becomes

@Configuration
public class SomeStepConfig {
    ...

    MyBatisCursorItemReader reader() {
        return new MyBatisCursorItemReaderBuilder<SomeIn>()
                .sqlSessionFactory(this.sqlSessionFactory)
                .queryId("selectSelectId")
                .parameterSupplier(() -> parameterMapFromLocalOrRemote())
                .build();
    }
    private Map<String, Object> parameterMapFromLocalOrRemote() {
        ...
    }

    ...
}

It works great so far, except that copying whole source of MyBatisCursorItemReaderBuilder & MyBatisCursorItemReader is not elegant enough. I considered subclassing, but the private fields makes it rather difficult.

Considering this is a very useful feature, at least for me, the changes are very small and simple, I was wondering if I could make a contribute. So I added the same feature to MyBatisPagingItemReaderBuilder & MyBatisPagingItemReader and made some changes to unit tests for the feature. Then there is this PR.

Thanks for you time for anyone review or read this PR.

@happysca
Copy link
Contributor Author

A shared Map instance might just work in my case (occurred to me just now hahahaha). Parameter supplier is not that important to me, but still more powerful than shared instances.

@kazuki43zoo kazuki43zoo self-requested a review September 25, 2022 16:40
@kazuki43zoo kazuki43zoo self-assigned this Sep 25, 2022
@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Sep 25, 2022
@kazuki43zoo kazuki43zoo added this to the 2.1.0 milestone Sep 25, 2022
Copy link
Member

@kazuki43zoo kazuki43zoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!! I think your suggestion is reasonable and useful. Could you confirm my trivial comments?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 89.345% when pulling 24b2b75 on happysca:batch_reader_query_parameter_supplier into 26d492c on mybatis:master.

@kazuki43zoo kazuki43zoo merged commit 5756a3e into mybatis:master Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants