Mybatis使用HashMap实现关系映射

一对多映射

<?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.test.mapper.QuestionInfoMapper">

    <resultMap id="QuestionInfoMap" type="java.util.HashMap">
        <result property="orderId" column="orderId"/>
        <result property="campaignId" column="campaignId"/>
        <result property="caller" column="called"/>
        <result property="called" column="called"/>
        <result property="agentId" column="agentId"/>
        <result property="callBeginTime" column="callBeginTime"/>
        <result property="callAnswerTime" column="callAnswerTime"/>
        <result property="callEndTime" column="callEndTime"/>
        <result property="callDuration" column="callDuration"/>
        <result property="callType" column="callType"/>
        <result property="callResult" column="callResult"/>
        <result property="questionnaireId" column="questionnaireId"/>

        <collection property="quesAnswer" ofType="java.util.HashMap" javaType="java.util.List">
            <result property="quesValue" column="quesValue"/>
            <result property="quesId" column="quesId"/>
            <result property="quesDesc" column="quesDesc"/>
        </collection>
    </resultMap>

    <select id="selectQuestionResultDetail" resultMap="QuestionInfoMap">
        select
            ifnull(a.order_id,'') as orderId,
            ifnull(a.task_id,'') as campaignId,
            ifnull(b.caller,'') as caller,
            ifnull(b.real_dnis,'') as called,
            ifnull(b.agent_id,'') as agentId,
            ifnull(date_format(b.call_begintime,'%y%m%d%h%i%s'),'') as callBeginTime,
            ifnull(date_format(b.call_out_succ_time,'%y%m%d%h%i%s'),'') as callAnswerTime,
            ifnull(date_format(b.call_endtime,'%y%m%d%h%i%s'),'') as callEndTime,
            ifnull((case when b.handup_time is not null or b.call_in_succ_time is not null
            then  convert(date_format(b.handup_time,'%y%m%d%h%i%s'),unsigned)
             - convert(date_format(b.call_in_succ_time,'%y%m%d%h%i%s'),unsigned)
            else 0 end ),'') as callDuration,
            ifnull(b.hangup_side,'') as callType,
            ifnull(b.call_result,'') as callResult,
            ifnull(a.questionnaire_id,'') as questionnaireId,
            ifnull(a.question_id,'') as quesId,
            ifnull(a.index_code,'') as quesValue,
            ifnull(a.question_answer_name,'') as quesDesc
        from ${tableName1} a
        left join ${tableName2} b on a.order_id = concat(b.proj_id, '_', b.taskitem_id)
        ${ew.getCustomSqlSegment}
    </select>
</mapper>

多对一映射

按查询嵌套方式

<?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.test.mapper.StudentMapper">

    <resultMap id="BaseStudentResult" type="java.util.HashMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--按照查询条件嵌套-->
        <association property="teacher" column="tid" javaType="java.util.HashMap" select="selectTeacherByTid">
            <result property="tid" column="id"/>
            <result property="tname" column="name"/>
        </association>
    </resultMap>

    <select id="selectStudentById" resultMap="BaseStudentResult" parameterType="java.lang.Integer">
        select
            id as id,
            name as name
        from students
        where id = #{id}
    </select>

    <select id="selectTeacherByTid" resultType="java.util.HashMap" parameterType="java.lang.Integer">
        select
            id as tid,
            name as tname
        from teachers
        where id = #{tid}
    </select>
</mapper>

按结果嵌套方式

<?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.test.mapper.StudentMapper">

    <resultMap id="BaseStudentResult" type="java.util.HashMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--按照结果嵌套-->
        <association property="teacher" javaType="java.util.HashMap">
            <result property="tid" column="id"/>
            <result property="tname" column="name"/>
        </association>
    </resultMap>

    
    <select id="selectStudentById" resultMap="BaseStudentResult" parameterType="java.lang.Integer">
        select
            a.id as id
            a.name as name,
            b.id as tid,
            b.name as tname
        from students a
        inner join teachers b on a.t_id = b.id
        where a.id = #{id}
    </select>
</mapper>

持续更新中。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌守

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值