MyBatis-Plus中SimpleQuery查詢實現(xiàn)
對list查詢后的結(jié)果用Stream流進行了一些封裝,使其可以返回一些指定結(jié)果,簡潔了api的調(diào)用,這種調(diào)用方式不用注入bean、不用注入bean、不用注入bean,通過實體類class查詢
**SimpleQuery.list()、SimpleQuery.keyMap()**較常用
<!--低版本沒有這個工具類-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>// 獲取ID
List<Long> list = SimpleQuery.list(new QueryWrapper<HssTypePropertyEntity>()
? ? .eq("type_id",5).lambda(), HssTypePropertyEntity::getId);
// 以typeId為key分組
Map<Long, HssEquipmentEntity> map =?
? ? SimpleQuery.keyMap(new QueryWrapper<HssEquipmentEntity>()
? ? .eq("type_id", 5).lambda(),
? ? HssEquipmentEntity::getTypeId);
// 查詢角色的隱藏ID,以角色ID分組
Map<Long, List<Long>> columnMap =
? ? SimpleQuery.group(new QueryWrapper<SysRoleColumnEntity>().lambda(),?
? ? SysRoleColumnEntity::getRoleId,?
? ? Collectors.mapping(SysRoleColumnEntity::getColumnId, Collectors.toList()));操作實例:
?// 我要這個表里對應(yīng)條件的用戶,用id作為key給我一個map
? ? ? ? Map<Long, Entity> idEntityMap = SimpleQuery.keyMap(Wrappers.<Entity>lambdaQuery().eq(Entity::getId, 1L), Entity::getId);
? ? ? ? // 校驗結(jié)果
? ? ? ? Entity entity = new Entity();
? ? ? ? entity.setId(1L);
? ? ? ? entity.setName("ruben");
? ? ? ? Assert.isTrue(idEntityMap.equals(Collections.singletonMap(1L, entity)), "Ops!");
? ? ? ? // 如果我只想要id和name組成的map
? ? ? ? Map<Long, String> idNameMap = SimpleQuery.map(Wrappers.lambdaQuery(), Entity::getId, Entity::getName);
? ? ? ? // 校驗結(jié)果
? ? ? ? Map<Long, String> map = new HashMap<>(1 << 2);
? ? ? ? map.put(1L, "ruben");
? ? ? ? map.put(2L, null);
? ? ? ? Assert.isTrue(idNameMap.equals(map), "Ops!");
? ? }
? ? @Test
? ? public void testGroup() {
? ? ? ? // 我需要相同名字的用戶的分為一組,再造一條數(shù)據(jù)
? ? ? ? doTestAutoCommit(m -> {
? ? ? ? ? ? Entity entity = new Entity();
? ? ? ? ? ? entity.setId(3L);
? ? ? ? ? ? entity.setName("ruben");
? ? ? ? ? ? m.insert(entity);
? ? ? ? });
? ? ? ? // 簡單查詢
? ? ? ? Map<String, List<Entity>> nameUsersMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName);
? ? ? ? // 校驗結(jié)果
? ? ? ? Map<String, List<Entity>> map = new HashMap<>(1 << 2);
? ? ? ? Entity chao = new Entity();
? ? ? ? chao.setId(2L);
? ? ? ? chao.setName(null);
? ? ? ? map.put(null, Collections.singletonList(chao));
? ? ? ? Entity ruben = new Entity();
? ? ? ? ruben.setId(1L);
? ? ? ? ruben.setName("ruben");
? ? ? ? Entity ruben2 = new Entity();
? ? ? ? ruben2.setId(3L);
? ? ? ? ruben2.setName("ruben");
? ? ? ? map.put("ruben", Arrays.asList(ruben, ruben2));
? ? ? ? Assert.isTrue(nameUsersMap.equals(map), "Ops!");
? ? ? ? // 解鎖高級玩法:
? ? ? ? // 獲取Map<name,List<id>>
? ? ? ? Map<String, List<Long>> nameIdMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName, Collectors.mapping(Entity::getId, Collectors.toList()));
? ? ? ? // 獲取Map<name,個數(shù)>
? ? ? ? Map<String, Long> nameCountMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName, Collectors.counting());
? ? ? ? // ...超多花樣
? ? }
? ? @Override
? ? protected String tableDataSql() {
? ? ? ? return "insert into entity(id,name) values(1,'ruben'),(2,null);";
? ? }
? ? @Override
? ? protected List<String> tableSql() {
? ? ? ? return Arrays.asList("drop table if exists entity", "CREATE TABLE IF NOT EXISTS entity (" +
? ? ? ? ? ? "id BIGINT NOT NULL," +
? ? ? ? ? ? "name VARCHAR(30) NULL DEFAULT NULL," +
? ? ? ? ? ? "PRIMARY KEY (id))");
? ? }當然原來的查詢也可以,只是還需要注入bean才能操作,listObjs(wrapper,mapper)
List<Long> strings =
hssTypePropertyService.listObjs(new QueryWrapper<HssTypePropertyEntity>()
.select("id").eq("type_id", 5)
,i->Long.valueOf(i.toString()));
ActiveRecord (查詢)模式
說明:
- 實體類只需繼承 Model 類即可進行強大的 CRUD 操作
- 需要項目中已注入對應(yīng)實體的BaseMapper
實體繼承 Model,調(diào)用自mapper,省去注入!
@Data
@TableName(value = "hss_history", autoResultMap = true)
public class HssHistoryEntity extends Model<HssHistoryEntity> implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
private Long id;
// json映射,autoResultMap必須開啟,寫了xml查詢需要resultMap映射字段
//查詢映射<result column="data" property="data" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler" javaType="com.alibaba.fastjson.JSONObject"/>
//更新映射#{e.data,jdbcType=VARCHAR,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},
@TableField(typeHandler = JacksonTypeHandler.class)
private JSONObject data;
}
創(chuàng)建對象直接就可以使用crud,省去注入
HssHistoryEntity entity = new HssHistoryEntity(); // 創(chuàng)建對象直接就有crud了 entity.insert(); entity.selectList(new QueryWrapper<HssHistoryEntity>()); entity.updateById(); entity.deleteById();
到此這篇關(guān)于MyBatis-Plus中SimpleQuery查詢實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis-Plus SimpleQuery查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 結(jié)合mybatis-plus實現(xiàn)簡單不需要寫sql的多表查詢
- mybatis-plus QueryWrapper自定義查詢條件的實現(xiàn)
- MyBatis-Plus 分頁查詢以及自定義sql分頁的實現(xiàn)
- MyBatis-Plus 如何實現(xiàn)連表查詢的示例代碼
- mybatis-plus分頁查詢的實現(xiàn)示例
- MyBatis-Plus 查詢指定字段的實現(xiàn)
- MyBatis-Plus多表聯(lián)查的實現(xiàn)方法(動態(tài)查詢和靜態(tài)查詢)
- mybatis-plus多表關(guān)聯(lián)查詢功能的實現(xiàn)
相關(guān)文章
springboot接入netty實現(xiàn)在線統(tǒng)計人數(shù)
本文主要介紹了springboot接入netty實現(xiàn)在線統(tǒng)計人數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03
SpringMVC?RESTFul及REST架構(gòu)風格介紹
這篇文章主要為大家介紹了SpringMVC?RESTFul及REST架構(gòu)風格介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Java自動化測試中多數(shù)據(jù)源的切換(實例講解)
下面小編就為大家?guī)硪黄狫ava自動化測試中多數(shù)據(jù)源的切換(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
微信小程序完整項目實戰(zhàn)記錄(前端+SpringBoot后端)
隨著微信小程序的流行,越來越多的開發(fā)者開始涉足小程序開發(fā),下面這篇文章主要給大家介紹了關(guān)于微信小程序完整項目實戰(zhàn)的相關(guān)資料,項目包括前端+SpringBoot后端,需要的朋友可以參考下2024-09-09
java中l(wèi)ist使用時需避免的場景總結(jié)
眾所周知,Java為開發(fā)者提供了多種集合類的實現(xiàn),其中幾乎所有業(yè)務(wù)代碼都需要用到List,但List的錯誤使用也會導致諸多問題,所以本文我們就來看一看幾個錯誤使用List的場景吧2023-10-10

