mybatis plus條件構造器
阿新 • • 發佈:2019-02-11
實體包裝器,用於處理 sql 拼接,排序,實體引數查詢等! 補充說明: 使用的是資料庫欄位,不是Java屬性! 實體包裝器 EntityWrapper 繼承 Wrapper 簡單示例 翻頁查詢 public Page<T> selectPage(Page<T> page, EntityWrapper<T> entityWrapper) { if (null != entityWrapper) { entityWrapper.orderBy(page.getOrderByField(), page.isAsc()); } page.setRecords(baseMapper.selectPage(page, entityWrapper)); return page; } 拼接 sql 方式 一 @Test public void testTSQL11() { /* * 實體帶查詢使用方法 輸出看結果 */ EntityWrapper<User> ew = new EntityWrapper<User>(); ew.setEntity(new User(1)); ew.where("user_name={0}", "'zhangsan'").and("id=1") .orNew("user_status={0}", "0").or("status=1") .notLike("user_nickname", "notvalue") .andNew("new=xx").like("hhh", "ddd") .andNew("pwd=11").isNotNull("n1,n2").isNull("n3") .groupBy("x1").groupBy("x2,x3") .having("x1=11").having("x3=433") .orderBy("dd").orderBy("d1,d2"); System.out.println(ew.getSqlSegment()); } 拼接 sql 方式 二 int buyCount = selectCount(Condition.create() .setSqlSelect("sum(quantity)") .isNull("order_id") .eq("user_id", 1) .eq("type", 1) .in("status", new Integer[]{0, 1}) .eq("product_id", 1) .between("created_time", startDate, currentDate) .eq("weal", 1)); 自定義 SQL 方法如何使用 Wrapper mapper java 介面方法 List<User> selectMyPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper); mapper xml 定義 <select id="selectMyPage" resultType="User"> SELECT * FROM user <where> ${ew.sqlSegment} </where> </select> 關於 ${ew.sqlSegment} 使用了 $ 不要誤以為就會被 sql 注入,請放心使用 mp 內部對 wrapper 進行了字元轉義處理! 條件引數說明 查詢方式 說明 setSqlSelect 設定 SELECT 查詢欄位 where WHERE 語句,拼接 + WHERE 條件 and AND 語句,拼接 + AND 欄位=值 andNew AND 語句,拼接 + AND (欄位=值) or OR 語句,拼接 + OR 欄位=值 orNew OR 語句,拼接 + OR (欄位=值) eq 等於= allEq 基於 map 內容等於= ne 不等於<> gt 大於> ge 大於等於>= lt 小於< le 小於等於<= like 模糊查詢 LIKE notLike 模糊查詢 NOT LIKE in IN 查詢 notIn NOT IN 查詢 isNull NULL 值查詢 isNotNull IS NOT NULL groupBy 分組 GROUP BY having HAVING 關鍵詞 orderBy 排序 ORDER BY orderAsc ASC 排序 ORDER BY orderDesc DESC 排序 ORDER BY exists EXISTS 條件語句 notExists NOT EXISTS 條件語句 between BETWEEN 條件語句 notBetween NOT BETWEEN 條件語句 addFilter 自由拼接 SQL last 拼接在最後,例如:last("LIMIT 1") 注意! xxNew 都是另起 ( ... ) 括號包裹。