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

关于in 空list的建议 #14

Closed
swellwu opened this issue Nov 7, 2017 · 5 comments
Closed

关于in 空list的建议 #14

swellwu opened this issue Nov 7, 2017 · 5 comments

Comments

@swellwu
Copy link

swellwu commented Nov 7, 2017

当where中出现in list时,如果list为empty list,会报错,能否针对这种情况在生成代码时进行处理,让in empty list时返回空。
mbg生成的相关代码:

								<when test="criterion.listValue">
									and ${criterion.condition}
									<foreach collection="criterion.value" item="listItem"
										open="(" close=")" separator=",">
										#{listItem}
									</foreach>
								</when>
@itfsw
Copy link
Owner

itfsw commented Nov 7, 2017

我没有怎么听懂你说的意思?
1.你的意思是list为empty的时候查询结果返回空?那为什么还要进查询呢,直接在代码里判断然后进行后续效率不是更高而且代码可读性更强?
2.还是说你是想实现如果list为empty就不加这个条件?这样你可以配合ExampleEnhancedPlugin插件这样写

userMapper.selectByExample(
        new UserExample()
                .createCriteria()
                .andIf(!CollectionUtils.isEmpty(userIds), add -> add.andUserIdIn(userIds))
                .example()
);

@swellwu
Copy link
Author

swellwu commented Nov 7, 2017 via email

@itfsw
Copy link
Owner

itfsw commented Nov 7, 2017

一般这种属于输入数据的异常了,如果是我处理可能会去使用验证框架,然后捕获验证异常,对于这些数据封装一个合理的返回吧。

@swellwu
Copy link
Author

swellwu commented Nov 7, 2017

也不是输入数据异常,就是涉及到多张表的搜索查询,每一个查询都会过滤,然后留下一个list来in,当查询条件比较多时,会出现多个是否为空的判断。

@itfsw
Copy link
Owner

itfsw commented Nov 8, 2017

这个已经进入方法内部逻辑实现了,也不可能在做切面之类的,可能只能通过一系列的判断来过滤了。虽然可以重写Criterion之类的来生成一些反向sql来实现你所描述的功能,但这毕竟会进入sql执行,效率肯定就比不上你外层的if判断了。目前好像也没有特别好的处理方式。

@itfsw itfsw closed this as completed Nov 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants