MyBatis高级结果映射

该博客介绍了MyBatis映射文件中的resultMap元素的使用,包括构造器、关联、集合和歧视等配置,用于处理一对多、多对一和复杂查询结果的映射。示例展示了如何将数据库查询结果映射到复杂的Java对象结构中,如Blog、Author和Post的关系映射。
摘要由CSDN通过智能技术生成

官网的示例

<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>

官网的例子实际使用中存在一些问题,比如collection标签中再嵌套,idea中没有提示,且查不出来;将嵌套的元素都改成resultmap后可用,记录如下;

例子

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.mapper.TargetTblMapper">
    <select id="getOneDayList" resultMap="EvaluateObjectMap">
        SELECT
            target_tbl.id AS T_id,
            target_tbl.sname AS T_sname,
            target_tbl.type AS T_type,
            target_tbl.serial_number AS T_serial_number,
            target_tbl.development_stage AS T_development_stage,
            target_tbl.code AS T_code,
            target_tbl.status AS T_status,
            test_info_tbl.id AS INFO_id,
            test_info_tbl.tester_number AS INFO_tester_number,
            test_info_tbl.target_tbl_id AS INFO_target_tbl_id,
            test_info_tbl.test_project AS INFO_test_project,
            test_info_tbl.test_type AS INFO_test_type,
            test_info_tbl.start_time AS INFO_start_time,
            test_info_tbl.end_time AS INFO_end_time,
            test_info_tbl.temprature AS INFO_temprature,
            test_info_tbl.humidity AS INFO_humidity,
            test_point_tbl.id AS POINT_id,
            test_point_tbl.test_point_name AS POINT_test_point_name,
            test_point_tbl.test_point_value AS POINT_test_point_value,
            test_point_tbl.test_info_tbl_id AS POINT_test_info_tbl_id,
            test_point_tbl.calibration_data AS POINT_calibration_data
        FROM
            target_tbl
                LEFT JOIN test_info_tbl ON target_tbl.id = test_info_tbl.target_tbl_id
                LEFT JOIN test_point_tbl ON test_info_tbl.id = test_point_tbl.test_info_tbl_id
        WHERE DATE_FORMAT(test_info_tbl.start_time,'%Y-%m-%d') = #{date}
    </select>

    <resultMap id="EvaluateObjectMap" type="com.xxx.dto.EvaluateObject">
        <id column="T_id"/>
        <association property="targetTbl" javaType="com.xxx.entity.TargetTbl">
            <id property="id" column="T_id"/>
            <result property="sname" column="T_sname"/>
            <result property="type" column="T_type"/>
            <result property="serialNumber" column="T_serial_number"/>
            <result property="developmentStage" column="T_development_stage"/>
            <result property="code" column="T_code"/>
            <result property="status" column="T_status"/>
        </association>
        <collection property="testItemList" ofType="com.xxx.dto.TestItem" resultMap="TestItemMap"/>
    </resultMap>

    <resultMap id="TestItemMap" type="com.xxx.dto.TestItem">
        <association property="testInfoTbl" resultMap="TestInfoTblMap"/>
        <collection property="testPointTblList" ofType="com.xxx.entity.TestPointTbl" resultMap="TestPointTblMap"/>
    </resultMap>

    <resultMap id="TestPointTblMap" type="com.xxx.entity.TestPointTbl">
        <id property="id" column="POINT_id"/>
        <result property="testPointName" column="POINT_test_point_name"/>
        <result property="testPointValue" column="POINT_test_point_value"/>
        <result property="testInfoTblId" column="POINT_test_info_tbl_id"/>
        <result property="calibrationData" column="POINT_calibration_data"/>
    </resultMap>

    <resultMap id="TestInfoTblMap" type="com.xxx.entity.TestInfoTbl">
        <id property="id" column="INFO_id"/>
        <result property="testerNumber" column="INFO_tester_number"/>
        <result property="targetTblId" column="INFO_target_tbl_id"/>
        <result property="testProject" column="INFO_test_project"/>
        <result property="testType" column="INFO_test_type"/>
        <result property="startTime" column="INFO_start_time"/>
        <result property="endTime" column="INFO_end_time"/>
        <result property="temprature" column="INFO_temprature"/>
        <result property="humidity" column="INFO_humidity"/>
    </resultMap>
</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值