zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    汇编四(习题)
    汇编子程序模块化(near&far)
    win10关闭防火墙
    python中numpy中的shape()的使用
    文件的拷贝linux命令
    python中的os.path.dirname(__file__)
    ubuntu系统下安装及查看opencv版本
    用git命令行克隆项目及出现failed解决方案
    ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '
    记录CenterNet代码编译成功运行
  • 原文地址:https://www.cnblogs.com/mkl7/p/12133502.html
Copyright © 2011-2022 走看看