MyBatisPlus的SimpleQuery如何实现?请详细讲解一下?

2026-04-02 12:581阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计888个文字,预计阅读时间需要4分钟。

MyBatisPlus的SimpleQuery如何实现?请详细讲解一下?

MyBatis-Plus的SimpleQuery查询实现简要介绍,以下为示例代码:

java// 示例:使用SimpleQuery查询用户列表public List listUsers() { QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.select(id, name, age); queryWrapper.where(age > {0}, 18); return userMapper.selectList(queryWrapper);}

该示例展示了如何通过SimpleQuery查询数据库中的用户列表,包括筛选条件和字段选择。对于需要深入了解的学习者或工作者,以下内容具有一定的参考价值:

- MyBatis-Plus的SimpleQuery简化了SQL查询的编写,通过链式调用方式实现复杂的查询需求。- 通过QueryWrapper可以方便地设置查询条件,包括字段选择、条件过滤等。- 对于大规模数据查询,可以使用Stream流处理查询结果,提高数据处理的效率。

需要的朋友请随意使用以下内容,对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 (查询)模式

说明:

MyBatisPlus的SimpleQuery如何实现?请详细讲解一下?

  • 实体类只需继承 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();

本文共计888个文字,预计阅读时间需要4分钟。

MyBatisPlus的SimpleQuery如何实现?请详细讲解一下?

MyBatis-Plus的SimpleQuery查询实现简要介绍,以下为示例代码:

java// 示例:使用SimpleQuery查询用户列表public List listUsers() { QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.select(id, name, age); queryWrapper.where(age > {0}, 18); return userMapper.selectList(queryWrapper);}

该示例展示了如何通过SimpleQuery查询数据库中的用户列表,包括筛选条件和字段选择。对于需要深入了解的学习者或工作者,以下内容具有一定的参考价值:

- MyBatis-Plus的SimpleQuery简化了SQL查询的编写,通过链式调用方式实现复杂的查询需求。- 通过QueryWrapper可以方便地设置查询条件,包括字段选择、条件过滤等。- 对于大规模数据查询,可以使用Stream流处理查询结果,提高数据处理的效率。

需要的朋友请随意使用以下内容,对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 (查询)模式

说明:

MyBatisPlus的SimpleQuery如何实现?请详细讲解一下?

  • 实体类只需继承 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();