一、entitywrapper多表查詢
entitywrapper 是 Mybatis-plus 框架提供的一個用於構建查詢條件的 API。相比於使用 XML 配置文件或者註解方式配置 SQL 語句,entitywrapper 具有更高的靈活性和易讀性。在多表查詢時,entitywrapper 非常方便。下面是一個示例:
// 使用 entitywrapper 進行多表查詢 EntityWrapper wrapper = new EntityWrapper(); wrapper.eq("user.id", 1L); wrapper.eq("order.status", "PAID"); List userList = userMapper.selectUserWithOrder(wrapper);
上面的代碼使用 entitywrapper 進行了 user 和 order 兩張表的多表查詢。其中,eq 方法指定了 user 和 order 表的查詢條件,最後使用 selectUserWithOrder 方法查詢結果。
使用 entitywrapper 可以方便地構建多表查詢條件,同時支持 SQL 語句中的各種查詢關鍵字和操作符。
二、entitywrapper和querywrapper
entitywrapper 和 querywrapper 都是 Mybatis-plus 提供的用於構建查詢條件的 API。它們之間的區別在於,entitywrapper 是以實體類為中心進行構建的,而 querywrapper 則是以純 SQL 語句為中心進行構建的。因此,在一些需要靈活構建查詢條件的場景下,querywrapper 顯得更加方便。
entitywrapper 和 querywrapper 的一個最大的區別在於,entitywrapper 可以與 Mybatis-plus 的 ActiveRecord 功能配合使用,使得代碼更加簡潔易讀。如下所示:
// 使用 entitywrapper 構建查詢條件 EntityWrapper wrapper = new EntityWrapper(); wrapper.eq("id", 1L); // 使用 ActiveRecord 進行查詢 User user = new User(); user.setName("Tom"); Wrapper queryWrapper = Wrappers.query().setEntity(user).and(wrapper); List userList = user.selectList(queryWrapper);
上面的代碼首先使用 entitywrapper 構建查詢條件,然後通過 Wrappers.query() 方法,使用 ActiveRecord 進行查詢。這樣,查詢條件就與 ActiveRecord 這個 ORM 框架緊密結合起來,使得代碼非常簡潔易讀。
三、entitywrapper方法
在實際開發中,我們可能需要對查詢的結果進行排序、分頁、去重等操作。entitywrapper 提供了一系列方法,供我們靈活地對查詢結果進行處理。
1. orderBy 方法
orderBy 方法可以對查詢結果進行排序。如下所示:
// 使用 entitywrapper 對查詢結果進行排序 EntityWrapper wrapper = new EntityWrapper(); wrapper.orderBy("age", true); List userList = userMapper.selectList(wrapper);
上面的代碼使用 entitywrapper 對查詢結果按照 age 字段進行升序排序。
2. groupBy 方法
groupBy 方法可以對查詢結果進行分組。如下所示:
// 使用 entitywrapper 對查詢結果進行分組 EntityWrapper wrapper = new EntityWrapper(); wrapper.groupBy("age"); List userList = userMapper.selectList(wrapper);
上面的代碼使用 entitywrapper 對查詢結果按照 age 字段進行分組。
3. distinct 方法
distinct 方法可以對查詢結果進行去重。如下所示:
// 使用 entitywrapper 對查詢結果進行去重 EntityWrapper wrapper = new EntityWrapper(); wrapper.setSqlSelect("distinct age"); List userList = userMapper.selectList(wrapper);
上面的代碼使用 entitywrapper 對查詢結果根據 age 字段進行去重。
四、entitywrapper清除條件
在使用 entitywrapper 構建查詢條件時,可能會有一些查詢條件是不必要的,此時需要將這些條件清除掉。Mybatis-plus 提供了如下方法實現清除:
// 清除所有查詢條件 EntityWrapper.clear(); // 清除指定查詢條件 EntityWrapper.clear(String column);
五、entitywrapper方法作用
entitywrapper 提供的方法非常豐富,幾乎能夠滿足所有的查詢條件構建需求。下面列舉一些常用的方法:
1. eq(String column, Object value):等於
2. ne(String column, Object value):不等於
3. like(String column, String value):模糊查詢
4. gt(String column, Object value):大於
5. lt(String column, Object value):小於
6. ge(String column, Object value):大於等於
7. le(String column, Object value):小於等於
8. between(String column, Object value1, Object value2):在某個區間內
9. in(String column, List<Object> valueList):在某個值列表中
10. notIn(String column, List<Object> valueList):不在某個值列表中
六、entitywrapper 獲取規定條數
在實際開發中,我們可能需要查詢結果的前幾條記錄,此時需要用到 limit 方法。Mybatis-plus 提供了如下方法實現:
// 查詢前5條記錄 EntityWrapper wrapper = new EntityWrapper(); wrapper.last("limit 5"); List userList = userMapper.selectList(wrapper);
上面的代碼使用 entitywrapper 對查詢結果進行限制,查詢結果只返回前5條記錄。
七、entitywrapper將條件至空
在使用 entitywrapper 構建查詢條件時,可能會有一些查詢條件是不必要的,此時需要將這些條件至空。Mybatis-plus 提供了如下方法實現:
// 將條件至空 EntityWrapper.where(String sql, Object... params);
其中,sql 參數表示 SQL 語句,params 參數表示佔位符的值。
八、總結
通過以上對 entitywrapper 的詳細闡述,我們可以看出,entitywrapper 是一個非常強大、靈活、易讀的 API。它支持多表查詢、查詢條件構建、查詢結果處理等多種操作,可以有效地提高我們的開發效率,同時也能讓我們的代碼更加清晰易懂。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248508.html