资讯详情

MyBatis-Plus,SimpleQuery查询

对list使用查询结果Stream流进行了一些包装,使其能够返回一些指定的结果,简洁api调用,这种调用方法,通过实体类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()));

 // 我要这个表里对应条件的用户,用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<name,List<id>>
        Map<String, List<Long>> nameIdMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName, Collectors.mapping(Entity::getId, Collectors.toList()));
        // 获取Map<name,个数>
        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()));

说明: ● 实体类只需继承 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映射字段
    //查询映射<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;  
}

创建对象直接就可以使用crud,省去注入

HssHistoryEntity entity = new HssHistoryEntity();
// 创建对象直接就有crud了
entity.insert();
entity.selectList(new QueryWrapper<HssHistoryEntity>());
entity.updateById();
entity.deleteById();

标签: 3l光点长距离高精度稳定传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台