mybatis example如何生成or条件的sql

我想利用mybatis的example类生成如下的sql语句:

SELECT * FROM User where nick_name like "%1502%" or real_name like "%1502%" or mobile like "%1502%"

我本来是这样使用的:

UserExample userExample = new UserExample();
UserExample.Criteria userCriteria = userExample.createCriteria();
userExample.or().andMobileLike("%1502%");
userExample.or().andRealNameLike("%1502%");
userExample.or().andNickNameLike("%1502%");
return userService.selectByExample(userExample);

但是这样使用并不能返回我想要的结果,它会把所有的User数据全部返回完全没有加入or限制笑哭
clipboard.png
求大神指教

阅读 12k
2 个回答

试试这个?

userExample.or().orMobileLike("%1502%");
userExample.or().orRealNameLike("%1502%");
userExample.or().orNickNameLike("%1502%");

试试这个:

userExample.or().andMobileLike("%1502%").andRealNameLike("%1502%").andNickNameLike("%1502%");
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题