2020/1/2 踩的关于Mybatis查询及封装的几个坑

1. 多表连接查询时  加括号

  <sql id="comEmpTables">
        (<include refid="empInfoTables"></include>,info_com_emp ce,data_job j)

        left join info_emp_train_history ieth on  i.id = ieth.empId
        left join info_emp_work_history iewh on  i.id  = iewh.empId
   </sql>

      不然会报1054错误

        错误代码: 1054

        Unknown column 'i.id' in 'on clause'

2. 多表连接 查询出来多条数据 且有重复 如何进行封装

SELECT 
 i.*
,ieth.trainYear,ieth.trainCon,ieth.trainHour,ieth.trainResult,ieth.trainDate,ieth.id AS tid
        ,iewh.workDate,iewh.workCom,iewh.workJob,iewh.workContent,iewh.id AS wid,i.id
FROM
  info_employee i 
  LEFT JOIN info_emp_train_history ieth 
    ON i.id = ieth.empId 
  RIGHT JOIN info_emp_work_history iewh 
    ON i.id = iewh.empId 
WHERE i.id = 28 

 

  四条数据都是属于一个人的   都有同一个id

  在总的resultMap中设置id 则该四条信息都会封装进一个对象 

<resultMap id="empResult" type="qdlb.edu_dev.modules.info.pojo.infoPojo.EmpInfoResult" autoMapping="true">
        <id column="id" property="id"></id>
<association property="employee" javaType="infoEmployee" > </association> <collection property="empTrainHistoryResults" resultMap="EmpTrainHistoryResult"> </collection> <collection property="empWorkHistoryResults" resultMap="EmpWorkHistoryResult"> </collection> </resultMap>

  

 

  在两个集合的resultMap中设置id

   则id不同的数据会只取重复中的一条封装进去

  id作为区分数据的关键,一个ID一条数据

  

  <resultMap id="EmpTrainHistoryResult" type="qdlb.edu_dev.modules.info.pojo.EmpTrainHistory.EmpTrainHistoryResult">
        <id column="tid" property="tid"></id>
        <result column="trainYear" property="trainYear"/>
        <result column="trainCon" property="trainCon"/>
        <result column="trainHour" property="trainHour"/>
        <result column="trainResult" property="trainResult"/>
        <result column="trainDate" property="trainDate"/>
    </resultMap>
    <resultMap id="EmpWorkHistoryResult" type="qdlb.edu_dev.modules.info.pojo.EmpWorkHistory.EmpWorkHistoryResult">
        <id column="wid" property="wid"></id>
        <result column="workDate" property="workDate"/>
        <result column="workCom" property="workCom"/>
        <result column="workJob" property="workJob"/>
        <result column="workContent" property="workContent"/>
    </resultMap>

 

posted @ 2020-01-02 16:05  萌胖胖  阅读(422)  评论(0编辑  收藏  举报