当前位置 : 主页 > 网络编程 > 其它编程 >

MyBatisPlus中SimpleQuery查询实现_java

来源:互联网 收集:自由互联 发布时间:2023-07-02
本文主要介绍了MyBatis-Plus中SimpleQuery查询实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着 对list查询后的结果用Stream流
本文主要介绍了MyBatis-Plus中SimpleQuery查询实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着

对list查询后的结果用Stream流进行了一些封装,使其可以返回一些指定结果,简洁了api的调用,这种调用方式不用注入bean、不用注入bean、不用注入bean,通过实体类class查询

**SimpleQuery.list()、SimpleQuery.keyMap()**较常用

com.baomidou mybatis-plus-boot-starter 3.5.1

// 获取IDList list = SimpleQuery.list(new QueryWrapper()    .eq("type_id",5).lambda(), HssTypePropertyEntity::getId);// 以typeId为key分组Map map =     SimpleQuery.keyMap(new QueryWrapper()    .eq("type_id", 5).lambda(),    HssEquipmentEntity::getTypeId);// 查询角色的隐藏ID,以角色ID分组Map columnMap =    SimpleQuery.group(new QueryWrapper().lambda(),     SysRoleColumnEntity::getRoleId,     Collectors.mapping(SysRoleColumnEntity::getColumnId, Collectors.toList()));

操作实例:

 // 我要这个表里对应条件的用户,用id作为key给我一个map        Map idEntityMap = SimpleQuery.keyMap(Wrappers.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 idNameMap = SimpleQuery.map(Wrappers.lambdaQuery(), Entity::getId, Entity::getName);        // 校验结果        Map 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 nameUsersMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName);        // 校验结果        Map 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 nameIdMap = SimpleQuery.group(Wrappers.lambdaQuery(), Entity::getName, Collectors.mapping(Entity::getId, Collectors.toList()));        // 获取Map        Map 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 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 strings = hssTypePropertyService.listObjs(new QueryWrapper() .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 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());entity.updateById();entity.deleteById();

网友评论