一对多映射
<?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>
持续更新中。。。