对list查询后的结果用Stream流进行了一些封装,使其可以返回一些指定结果,简洁了api的调用,这种调用方式不用注入bean、不用注入bean、不用注入bean,通过实体类class查询
**SimpleQuery.list()、SimpleQuery.keyMap()**较常用
// 获取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()));
*** 作实例:
// 我要这个表里对应条件的用户,用id作为key给我一个map
Map<Long, Entity> idEntityMap = SimpleQuery.keyMap(Wrappers.<Entity>lambdaQuery().eq(Entity::getId, 1L), Entity::getId);
// 校验结果
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);
// 校验结果
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() {
// 我需要相同名字的用户的分为一组,再造一条数据
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);
// 校验结果
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>
Map<String, List<Long>> nameIdMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName, Collectors.mapping(Entity::getId, Collectors.toList()));
// 获取Map
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 *** 作
● 需要项目中已注入对应实体的BaseMapper
实体继承 Model,调用自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映射字段
//查询映射
//更新映射#{e.data,jdbcType=VARCHAR,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},
@TableField(typeHandler = JacksonTypeHandler.class)
private JSONObject data;
}
创建对象直接就可以使用crud,省去注入
HssHistoryEntity entity = new HssHistoryEntity();
// 创建对象直接就有crud了
entity.insert();
entity.selectList(new QueryWrapper<HssHistoryEntity>());
entity.updateById();
entity.deleteById();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)