mybatis的多表查詢映射問題


mybatis的多表映射:

  表字段映射實體的幾種情況:

   單表查詢

    第一種。起別名:  起的別名一定是對應的實體字段名。

      

SELECT
    t_p.param_id paramId, 
    t_p.station_id stationId,
    t_p.station_name stationName,
    t_p.pump_id pumpId,
    t_p.vibrate vibrate,
    t_p.temperature temperature,
    t_p.report_time reportTimeP   
FROM
    dbo.tb_param AS t_p
    

    第二種:mybatis的駝峰命名對應

    

<configuration>
    <settings>  
        <setting name="mapUnderscoreToCamelCase" value="true" />  
    </settings>
</configuration>

這種適合param_id 對應 實體中的 paramId

   多表查詢  

  

<!--泵機基本參數-->
    <resultMap type="PLCParam" id="commentResult">
        <id property="paramId" column="param_id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="pumpId" column="pump_id"/>
        <result property="vibrate" column="vibrate"/>
        <result property="temperature" column="temperature"/>
        <result property="reportTime" column="report_time" jdbcType="TIMESTAMP"/>
        <association property="electricity"  resultMap="plcelectricityResult"/>
        <association property="voltage"  resultMap="plcvoltageResult"/>
        <association property="flow"  resultMap="plcflowResult"/>
        <association property="status"  resultMap="plcstatusResult"/>
    </resultMap>
    <!--電流表-->
    <resultMap type="PLCElectricity" id="plcelectricityResult">
        <id property="electriId" column="electri_id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="pumpId" column="pump_id"/>
        <result property="electriOne" column="electri_one"/>
        <result property="electriTwo" column="electri_two"/>
        <result property="electriThree" column="electri_three"/>
        <result property="reportTimeE" column="report_time" jdbcType="TIMESTAMP"/>
    </resultMap>
    <!--電壓表-->
    <resultMap type="PLCVoltage" id="plcvoltageResult">
        <id property="voltageId" column="voltage_id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="pumpId" column="pump_id"/>
        <result property="voltageOne" column="voltage_one"/>
        <result property="voltageTwo" column="voltage_two"/>
        <result property="voltageThree" column="voltage_three"/>
        <result property="reportTimeV" column="report_time"/>
    </resultMap>
    <!--flow流量表-->
    <resultMap type="PLCFlow" id="plcflowResult">
        <id property="flowId" column="flow_id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="pumpId" column="pump_id"/>
        <result property="inputFlow" column="input_flow"/>
        <result property="outputFlow" column="output_flow"/>
        <result property="reportTimeF" column="report_time" jdbcType="TIMESTAMP"/>
    </resultMap>
    <!--狀態表-->
    <resultMap type="PLCStatus" id="plcstatusResult">
        <id property="paramId" column="param_id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="pumpId" column="pump_id"/>
        <result property="status" column="status"/>
        <result property="lastUpdateTime" column="last_update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--泵機的基本參數-->
    <select id="queryPlcParam" resultMap="commentResult">
      SELECT
            t_p.param_id,/*參數表id*/
            t_p.station_id,/*泵站id*/
            t_p.station_name,/*泵站名稱*/
            t_p.pump_id ,/*泵機id*/
            t_p.vibrate,/*泵機振動*/
            t_p.temperature,/*泵機溫度*/
            t_p.report_time,/*參數表上報時間*/
            t_f.input_flow,/*進口流量*/
            t_f.output_flow,/*出口流量*/
            t_f.report_time,/*流量表上報時間*/
            t_s.status,/*泵機狀態 0:啟動,1:停止,2:故障*/
            t_s.last_update_time,/*最后更新時間*/
            t_v.voltage_one,/*電壓數據1*/
            t_v.voltage_two,/*電壓數據2*/
            t_v.voltage_three,/*電壓數據3*/
            t_v.report_time,/*電壓上報時間*/
            t_e.electri_one,/*電流數據1*/
            t_e.electri_two,/*電流數據2*/
            t_e.electri_three,/*電流數據3*/
            t_e.report_time/*電流上報時間*/
      FROM
        dbo.tb_param AS t_p
            LEFT JOIN dbo.tb_flow AS t_f ON t_f.station_id = t_p.station_id
            AND t_f.pump_id = t_p.pump_id
            LEFT JOIN dbo.tb_status AS t_s ON t_s.station_id = t_p.station_id
            AND t_s.pump_id = t_p.pump_id
            LEFT JOIN dbo.tb_voltage AS t_v ON t_v.station_id = t_p.station_id
            AND t_v.pump_id = t_p.pump_id
            LEFT JOIN dbo.tb_electricity AS t_e ON t_e.pump_id = t_p.pump_id
            AND t_e.station_id = t_p.station_id

    </select>

  使用resultMap接收不用指定列別名。如果指定列別名,屬於駝峰命名格式的字段映射為空

  實體如下:

  

 /**自動增長ID,數據表主鍵*/
    private int paramId;
    /**泵站編號*/
    private int stationId;
    /**泵站name*/
    private String stationName;
    /**泵機id*/
    private int pumpId;
    /**泵機振動 */
    private String vibrate;
    /**泵機溫度 */
    private String temperature;
    /**上報時間*/
    private Date reportTime;

    private PLCElectricity electricity;

    private PLCFlow flow;

    private PLCStatus status;

    private PLCVoltage voltage;

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM