Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for allowing null value on foreach tag #1883

Merged
merged 1 commit into from
Dec 21, 2021

Conversation

kazuki43zoo
Copy link
Member

@kazuki43zoo kazuki43zoo commented Mar 28, 2020

I will propose to support new option for allowing null value on collection attribute of foreach tag.

In this change, a developer can omit the if tag as follow:

  • current version
<if test="friendList != null"> <!-- Need null check using 'if' tag -->
  <foreach item="item" collection="friendList" open="id in (" separator="," close=")">
    #{item.id}
  </foreach>
</if>
  • new version
<!-- <if test="friendList != null"> -->
<!-- Specify 'nullable="true"' instead of 'if' tag -->
<foreach item="item" collection="friendList" nullable="true" open="id in (" separator="," close=")">
  #{item.id}
</foreach>
<!-- </if> -->

Also, a developer can change the default nullable value when the nullable attribute is omitted using global configuration as follow:

  • global configuration

XML configuration (mybatis-config.xml):

<settings>
  <setting name="nullableOnForEach" value="true"/>
</settings>

or

Java configuration(Configuration class):

Configuration mybatisConfiguration = new Configuration();
mybatisConfiguration.setNullableOnForEach(true);
  • SQL definition
<!-- <if test="friendList != null"> -->
<!-- Can omit 'if' tag and 'nullable' attribute both -->
<foreach item="item" collection="friendList" open="id in (" separator="," close=")">
  #{item.id}
</foreach>
<!-- </if> -->

WDYT?

@kazuki43zoo
Copy link
Member Author

@mybatis/committers WDYT this PR?

@harawata
Copy link
Member

Thank you for your work, @kazuki43zoo !

This gets a few upvotes and the impact looks small, so I'm OK with it.

@kazuki43zoo
Copy link
Member Author

@harawata Thx for your reaction!! I will rebase this PR.

@kazuki43zoo kazuki43zoo self-assigned this Dec 19, 2021
@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Dec 19, 2021
@kazuki43zoo kazuki43zoo added this to the 3.5.9 milestone Dec 19, 2021
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 87.271% when pulling 7738026 on kazuki43zoo:allow-null-on-forEach into 6490a16 on mybatis:master.

@kazuki43zoo
Copy link
Member Author

@harawata I've rebased and update doc.

@harawata
Copy link
Member

Thank you, @kazuki43zoo !
LGTM 👍

@kazuki43zoo kazuki43zoo merged commit 253982f into mybatis:master Dec 21, 2021
@kazuki43zoo kazuki43zoo deleted the allow-null-on-forEach branch December 21, 2021 22:57
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