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;
