Mybatis plus like查詢問題
又談到了Mybatis plus了,真的很是痛心疾首,在做模糊查詢的時候出現了一個問題,版本還是2.0.8,我覺得大問題沒有小問題不斷吧!
上代碼
@MethodAnnot()
public static List<VwUserRole> getVwUserRoleByList(VwUserRole vwUserRole){
return vwUserRoleService.selectList(
new EntityWrapper<VwUserRole>(vwUserRole).like("username",vwUserRole.getUsername(), SqlLike.DEFAULT));
}
模糊查詢USER,講道理來說是沒有問題的,下面我們查一下這個SQL是什麼
SELECT id,username,del_flag AS delFlag FROM vw_user_role WHERE username='a' AND del_flag='0' AND (username LIKE '%a%')
應該一下就可以看的出來 這樣的查詢語法怎麼可能查詢的出來數據呢?我覺得這算是一個Mybatis plus的Bug的吧!
解決方法也很簡單,換個寫法
@MethodAnnot()
public static List<VwUserRole> getVwUserRoleByList(VwUserRole vwUserRole){
EntityWrapper ew=new EntityWrapper();
ew.setEntity(new VwUserRole());
ew.eq("del_flag",'0');
ew.like("username",vwUserRole.getUsername(), SqlLike.DEFAULT);
return vwUserRoleService.selectList(ew);
}
我們看一下這樣查詢的SQL是什麼
SELECT id,username,del_flag AS delFlag FROM vw_user_role WHERE (del_flag = 0 AND username LIKE '%a%')
這樣是一點問題也沒有的哦,應該能看出問題在那裏吧?哈哈哈,不說了。
感覺還有好多地方會有坑等着我,踩一個坑記錄一下吧!!!
mybatis-plus官方:http://baomidou.oschina.io/mybatis-plus-doc/