java mybatis plus 树结构、联表查询、查询语句相互调用

这篇博客讨论了如何在MyBatis中使用resultMap处理返回的数据结构,特别是当返回的数据包含列表元素且这些元素也需要进行自动聚合时。通过示例代码展示了如何配置resultMap,以实现一对多的层级数据列表结构的自动聚合。重点在于理解并正确使用resultMap的collection属性和select子元素,确保数据能够正确映射和层级化。
摘要由CSDN通过智能技术生成

实体VO,即返回的数据实体

@ApiModel(" 区域 间隔 表")
public class PlaceVo implements Serializable {
   
    @ApiModelProperty(value = "子类区域间隔", dataType = "List")
    List<PlaceVo> subList;

    @ApiModelProperty(value = "被测设备表")
    List<PrimaryDeviceVo> primaryDeviceList;

    private Long placeId;
 
    @ApiModelProperty(value = "名称")
    private String placeName;
}


@Data
public class PrimaryDeviceVo extends PrimaryDevice {
    @ApiModelProperty(value = "被测设备部件表")
    List<PartsVo> partsList;

    @ApiModelProperty(value = "被测设备id")
    private Long id;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "编号")
    private String code;
}

@Data
@ApiModel(" 区域 间隔 表")
public class PartsVo implements Serializable {

    @ApiModelProperty(value = "部位id")
    private Long id;

    @ApiModelProperty(value = "名称")
    private String name;

}




    public List<PlaceVo> listTreeAll() {
//        List<PrimaryDeviceVo> primaryDeviceVos = primaryDeviceMapper.listTree(1L);
        return baseMapper.listTreeAll(0L);
    }

List<PlaceVo> listTreeAll(Long parentId);


<mapper namespace="com.into.smart.ele.manager.mapper.PlaceMapper">

    <resultMap id="deviceTreeEntity" type="com.into.smart.ele.manager.entity.vo.PlaceVo">
        <id property="placeId" column="place_id"/>
        <id property="placeName" column="place_name"/>
        <id property="parentId" column="parent_id"/>
        <!--collection:一对多
            assocication:一对一
            -->

        <collection property="subList" column="place_id"
                    ofType="com.into.smart.ele.manager.entity.vo.PlaceVo"
                    select="com.into.smart.ele.manager.mapper.PlaceMapper.listTreeAll">
        </collection>
        <collection property="primaryDeviceList" column="place_id"
                    ofType="com.into.smart.ele.manager.entity.vo.PrimaryDeviceVo"
                    select="com.into.smart.ele.manager.mapper.PrimaryDeviceMapper.listTree">
        </collection>
    </resultMap>

    <select id="listTreeAll" resultMap="deviceTreeEntity">
        SELECT epi.place_id,epi.place_name,epi.parent_id
        from ele_place_info epi
        <where>
            <if test="parentId != null">parent_id=#{parentId}</if>
        </where>
    </select>
</mapper>


被调用的另一个mapper

@Mapper
public interface PrimaryDeviceMapper extends BaseMapper<PrimaryDevice> {

    List<PrimaryDeviceVo> getList(@Param("params") PrimaryDeviceDto params);

    /**
     * 设备部位树
     *
     * @return
     */
    List<PrimaryDeviceVo> listTree(Long placeId);
}



<mapper namespace="com.into.smart.ele.manager.mapper.PrimaryDeviceMapper">
    <resultMap id="deviceTreeEntity" type="com.into.smart.ele.manager.entity.vo.PrimaryDeviceVo">
        <!--        <id property="factory.name" column="factory.name"/>-->
        <!--        <id property="site.siteName" column="site.siteName"/>-->
        <!--        <result property="alarmEvents" column="alarm_events"/>-->
        <!--collection:一对多
            assocication:一对一
            -->

        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="partsList" ofType="com.into.smart.ele.manager.entity.vo.PartsVo">
            <result property="id" column="c_t_id"/>
            <result property="name" column="c_t_name"/>
        </collection>

    </resultMap>

    <select id="getList" resultType="com.into.smart.ele.manager.entity.vo.PrimaryDeviceVo">
        SELECT
        pinfo.place_name,
        jinfo.place_name AS jgName,
        dtype.type_level,
        dtype.type_name,
        dinfo.*
        FROM
        ele_place_info pinfo
        LEFT JOIN ele_place_info jinfo ON pinfo.place_id = jinfo.parent_id
        INNER JOIN assist_primary_device dinfo ON pinfo.place_id = dinfo.place_id
        INNER JOIN ele_primary_device_type dtype ON dtype.primary_device_type_id = dinfo.type_id
        <where>
            <if test="params.deviceProperties==1">
                and (dtype.type_level=1 or dtype.type_level=2)
            </if>
            <if test="params.deviceProperties==2">
                and (dtype.type_level=3)
            </if>
            <if test="params.placeId!=null">
                and (pinfo.place_id=#{params.placeId} or pinfo.parent_id=#{params.placeId})
            </if>
        </where>

    </select>

    <select id="listTree" resultMap="deviceTreeEntity">
        SELECT ap.id c_t_id, ap.name c_t_name,
        apd.id, apd.name,apd.place_id
        from assist_primary_device apd
        LEFT JOIN assist_primary_device_parts apdp on apdp.from_parimary_deivce_id=apd.id
        left join assist_parts ap on apdp.parts_id=ap.id
        <where>
            <if test="placeId != null">apd.place_id=#{placeId}</if>
        </where>
    </select>
</mapper>



注意事项

  • 返回的数据结构是列表,并且列表的元素也包含需要自动聚合的其他实体列表数据时,mybatis的select标签需要用resultMap来承接返回的,并用来映射数据结构,resultType没有聚合数据的效果,即转不了一对多的层级数据列表结构
  • 正确示例:\<select id=“listTree” resultMap=“deviceTreeEntity”>
### 回答1: 在 MyBatis Plus 中,可以使用 `join` 子句在两张或多张表之间进行联表查询。 假设你想要在用户表和订单表之间进行联表查询,可以使用如下 SQL 语句: ``` SELECT * FROM users u JOIN orders o ON u.id = o.user_id ``` 要在 MyBatis Plus 中执行此查询,需要创建一个查询方法,并使用 `@Select` 注解将其映射到相应的 SQL 语句。 下面是一个示例: ```java @Mapper public interface OrderMapper { @Select("SELECT * FROM users u JOIN orders o ON u.id = o.user_id") List<Map<String, Object>> selectUsersAndOrders(); } ``` 上述代码将返回一个包含用户和订单信息的列表。你也可以使用自定义的实体类来映射结果,而不是使用 `Map`。 在 MyBatis Plus 中,你还可以使用 `@Results` 和 `@Result` 注解来映射查询结果到实体类的属性,以及使用 `@One` 和 `@Many` 注解进行一对一或一对多的关联映射。 希望这对你有帮助。 ### 回答2: Mybatis-Plus是一个基于Mybatis的增强工具,它提供了很多方便的功能,使得在使用Mybatis进行数据库操作时更加简单和高效。使用Mybatis-Plus实现联表查询可以按照以下步骤进行: 首先,需要根据需要创建对应的实体类,并使用注解@Table来指定对应的数据库表名。在实体类中可以使用注解@JoinColumn来指定关联字段。 例如:假设有两个实体类User和Order,User和Order是通过UserId关联的,可以使用注解@JoinColumn(name="UserId")来指定关联字段。 接下来,在mapper接口中,使用@Mapper注解标注接口,并添加对应的方法用于联表查询。可以使用注解@SelectProvider来自定义SQL语句,也可以使用@Query注解来直接编写SQL语句。 例如:假设要查询用户信息以及用户的订单信息,可以在mapper接口中添加如下方法: @Select("SELECT u.*, o.* FROM user u LEFT JOIN order o ON u.id = o.user_id WHERE u.id = #{id}") List<UserOrderDto> getUserOrderList(@Param("id") Long id); 然后,在Service层中调用mapper接口中的方法来进行联表查询。可以直接调用mapper接口中的方法,也可以使用@AutoWired注解将mapper接口注入到Service中进行调用。 最后,在Controller层中,调用Service层中的方法获取联表查询的结果,并将结果返回给前端。 以上就是使用Mybatis-Plus实现联表查询的基本步骤。通过使用Mybatis-Plus,我们可以更加简单、高效地实现复杂的联表查询操作。 ### 回答3: MyBatis-Plus 是一个基于 MyBatis 的增强工具,它提供了许多便捷的功能,其中包括实现联表查询。 要使用 MyBatis-Plus 实现联表查询,我们需要按照以下步骤进行操作: 1. 创建实体类: 首先,我们需要创建对应的实体类,分别对应数据库中的每个表。每个类需要使用 `@TableName` 注解来指定对应的数据库表名,并使用 `@TableField` 注解指定实体类属性与数据库字段的映射关系。 2. 创建Mapper接口: 接下来,我们需要创建一个继承自 Mapper 接口的自定义接口。在自定义接口中,我们可以定义多个用于联表查询的方法,并使用 MyBatis-Plus 提供的注解进行配置。 3. 实现联表查询: 在自定义接口中,我们可以使用 MyBatis-Plus 的注解来配置联表查询操作。比如,我们可以使用 `@TableId`、`@TableField`、`@Query`、`@Join` 等注解来实现多表关联查询操作。 其中,`@Join` 注解用于指定多表关联查询的条件,`@Query` 注解用于编写 SQL 查询语句。 4. 调用联表查询方法: 最后,我们可以在业务逻辑层的具体方法中调用实现好的联表查询方法。通过调用方法即可得到联表查询结果。 通过以上步骤,我们可以轻松地使用 MyBatis-Plus 实现联表查询MyBatis-Plus 提供了一系列强大的注解,可以帮助我们简化 SQL 语句的编写,并提高查询效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值