-
Notifications
You must be signed in to change notification settings - Fork 398
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
Comments
我没有怎么听懂你说的意思? userMapper.selectByExample(
new UserExample()
.createCriteria()
.andIf(!CollectionUtils.isEmpty(userIds), add -> add.andUserIdIn(userIds))
.example()
); |
我的意思就是第一个意思,现在是在代码里判断list是否为空,但是如果忘了判断传入了空的list,mysql解析会报错的。站用户角度,in空list应该是返回空而不是直接报错 。
现在项目中是判断空,但是这种地方还挺多的,感觉代码不太整洁,如果忘了还会直接抛异常。除了代码判断是否为空,还有别的更整洁的方式吗?
这个确实是不太属于mbg的范畴,我是想交流有没有更好的建议。
发自网易邮箱大师
在2017年11月7日 17:45,何伟<notifications@github.com> 写道:
我没有怎么听懂你说的意思?
1.你的意思是list为empty的时候查询结果返回空?那为什么还要进查询呢,直接在代码里判断然后进行后续效率不是更高而且代码可读性更强?
2.还是说你是想实现如果list为empty就不加这个条件?这样你可以配合ExampleEnhancedPlugin插件这样写
userMapper.selectByExample(
newUserExample()
.createCriteria()
.andIf(!CollectionUtils.isEmpty(userIds), add -> add.andUserIdIn(userIds))
.example()
);
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
一般这种属于输入数据的异常了,如果是我处理可能会去使用验证框架,然后捕获验证异常,对于这些数据封装一个合理的返回吧。 |
也不是输入数据异常,就是涉及到多张表的搜索查询,每一个查询都会过滤,然后留下一个list来in,当查询条件比较多时,会出现多个是否为空的判断。 |
这个已经进入方法内部逻辑实现了,也不可能在做切面之类的,可能只能通过一系列的判断来过滤了。虽然可以重写Criterion之类的来生成一些反向sql来实现你所描述的功能,但这毕竟会进入sql执行,效率肯定就比不上你外层的if判断了。目前好像也没有特别好的处理方式。 |
当where中出现in list时,如果list为empty list,会报错,能否针对这种情况在生成代码时进行处理,让in empty list时返回空。
mbg生成的相关代码:
The text was updated successfully, but these errors were encountered: