MyBatis-Plus中SimpleQuery查詢實(shí)現(xiàn)
對(duì)list查詢后的結(jié)果用Stream流進(jìn)行了一些封裝,使其可以返回一些指定結(jié)果,簡(jiǎn)潔了api的調(diào)用,這種調(diào)用方式不用注入bean、不用注入bean、不用注入bean,通過(guò)實(shí)體類class查詢
**SimpleQuery.list()、SimpleQuery.keyMap()**較常用
<!--低版本沒(méi)有這個(gè)工具類-->
<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,以角色I(xiàn)D分組
Map<Long, List<Long>> columnMap =
? ? SimpleQuery.group(new QueryWrapper<SysRoleColumnEntity>().lambda(),?
? ? SysRoleColumnEntity::getRoleId,?
? ? Collectors.mapping(SysRoleColumnEntity::getColumnId, Collectors.toList()));操作實(shí)例:
?// 我要這個(gè)表里對(duì)應(yīng)條件的用戶,用id作為key給我一個(gè)map
? ? ? ? Map<Long, Entity> idEntityMap = SimpleQuery.keyMap(Wrappers.<Entity>lambdaQuery().eq(Entity::getId, 1L), Entity::getId);
? ? ? ? // 校驗(yàn)結(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);
? ? ? ? // 校驗(yàn)結(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);
? ? ? ? });
? ? ? ? // 簡(jiǎn)單查詢
? ? ? ? Map<String, List<Entity>> nameUsersMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName);
? ? ? ? // 校驗(yàn)結(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!");
? ? ? ? // 解鎖高級(jí)玩法:
? ? ? ? // 獲取Map<name,List<id>>
? ? ? ? Map<String, List<Long>> nameIdMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName, Collectors.mapping(Entity::getId, Collectors.toList()));
? ? ? ? // 獲取Map<name,個(gè)數(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))");
? ? }當(dāng)然原來(lái)的查詢也可以,只是還需要注入bean才能操作,listObjs(wrapper,mapper)
List<Long> strings =
hssTypePropertyService.listObjs(new QueryWrapper<HssTypePropertyEntity>()
.select("id").eq("type_id", 5)
,i->Long.valueOf(i.toString()));
ActiveRecord (查詢)模式
說(shuō)明:
- 實(shí)體類只需繼承 Model 類即可進(jìn)行強(qiáng)大的 CRUD 操作
- 需要項(xiàng)目中已注入對(duì)應(yīng)實(shí)體的BaseMapper
實(shí)體繼承 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必須開(kāi)啟,寫(xiě)了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)建對(duì)象直接就可以使用crud,省去注入
HssHistoryEntity entity = new HssHistoryEntity(); // 創(chuàng)建對(duì)象直接就有crud了 entity.insert(); entity.selectList(new QueryWrapper<HssHistoryEntity>()); entity.updateById(); entity.deleteById();
到此這篇關(guān)于MyBatis-Plus中SimpleQuery查詢實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis-Plus SimpleQuery查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 結(jié)合mybatis-plus實(shí)現(xiàn)簡(jiǎn)單不需要寫(xiě)sql的多表查詢
- mybatis-plus QueryWrapper自定義查詢條件的實(shí)現(xiàn)
- MyBatis-Plus 分頁(yè)查詢以及自定義sql分頁(yè)的實(shí)現(xiàn)
- MyBatis-Plus 如何實(shí)現(xiàn)連表查詢的示例代碼
- mybatis-plus分頁(yè)查詢的實(shí)現(xiàn)示例
- MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)
- MyBatis-Plus多表聯(lián)查的實(shí)現(xiàn)方法(動(dòng)態(tài)查詢和靜態(tài)查詢)
- mybatis-plus多表關(guān)聯(lián)查詢功能的實(shí)現(xiàn)
相關(guān)文章
springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù)
本文主要介紹了springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
java RocketMQ快速入門(mén)基礎(chǔ)知識(shí)
這篇文章主要介紹了java RocketMQ快速入門(mén)基礎(chǔ)知識(shí),所以RocketMQ是站在巨人的肩膀上(kafka),又對(duì)其進(jìn)行了優(yōu)化讓其更滿足互聯(lián)網(wǎng)公司的特點(diǎn)。它是純Java開(kāi)發(fā),具有高吞吐量、高可用性、適合大規(guī)模分布式系統(tǒng)應(yīng)用的特點(diǎn)。,需要的朋友可以參考下2019-06-06
SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹
這篇文章主要為大家介紹了SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Java利用poi讀取Excel詳解實(shí)現(xiàn)
Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java對(duì)Microsoft Office格式檔案讀和寫(xiě)的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫(xiě),意為簡(jiǎn)潔版的模糊實(shí)現(xiàn)2022-07-07
微信小程序完整項(xiàng)目實(shí)戰(zhàn)記錄(前端+SpringBoot后端)
隨著微信小程序的流行,越來(lái)越多的開(kāi)發(fā)者開(kāi)始涉足小程序開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于微信小程序完整項(xiàng)目實(shí)戰(zhàn)的相關(guān)資料,項(xiàng)目包括前端+SpringBoot后端,需要的朋友可以參考下2024-09-09
java中l(wèi)ist使用時(shí)需避免的場(chǎng)景總結(jié)
眾所周知,Java為開(kāi)發(fā)者提供了多種集合類的實(shí)現(xiàn),其中幾乎所有業(yè)務(wù)代碼都需要用到List,但List的錯(cuò)誤使用也會(huì)導(dǎo)致諸多問(wèn)題,所以本文我們就來(lái)看一看幾個(gè)錯(cuò)誤使用List的場(chǎng)景吧2023-10-10

