1对多: a表数据对b表多条(a_id) a表数据对c表多条(a_id) 关联字段都是(a_id)做的比喻
XML
<resultMap> <collection property="deviceParamsList" javaType="java.util.List" ofType="com.jc.local.entity.DeviceParam" select="com.jc.local.mapper.DeviceMapper.selectOutput" 注意看这里<select>这个标签路径,写道当前的Maaper.java路径,如果在其他Mapper中写这个接口方法,会映射不到,服务报错。 column="number"> <column>这个标签:《需要给一个参数》也就是关联条件的a_id </collection> <collection property="deviceOutputList" javaType="java.util.List" ofType="com.jc.local.entity.DeviceOutput" select="com.jc.local.mapper.DeviceMapper.selectParam" column="number"> </collection> </resultMap> <select id="numberJoinOutPutJoinParamList" resultMap="BaseResultMapNumber" parameterType="java.lang.String" > //结果类型时封装好的那个Map的id="名称” 拿到这个结果集进行查询 SELECT * FROM device d where d.NUMBER = #{number} </select> <select id="selectParam" parameterType="java.lang.String" resultType="com.jc.local.entity.DeviceParam" > //结果类型时B表的实体路径 select * from device_param where DEVICE_NUM = #{number} </select> <select id="selectOutput" parameterType="java.lang.String" resultType="com.jc.local.entity.DeviceOutput"> //结果类型时C表实体路径 select * from device_output where DEVICE_NUM = #{number} </select>mapper.java
package com.jc.local.mapper; 接口路径:com.jc.local.mapper.DeviceMapper 接口名:DeviceMapper //a关联B也关联c查询 List<Device> numberJoinOutPutJoinParamList(String number); List<DeviceOutput> selectOutput(String number); List<DeviceParam> selectParam(String number);controller.java
返回值给List<>就行总结: 之前的sql关联两个子表,代码走到第一个子表的时候就会生成一个结果集。然后用第一个生成的结果集,去映射关联第二个那个子表a_id(形成翻倍)
SELECT d.* FROM device d 关联第一个子表 LEFT JOIN device_param p ON d.NUMBER = p.DEVICE_NUM 关联第二个子表 LEFT JOIN device_output o ON d.NUMBER = o.DEVICE_NUM WHERE d.NUMBER = 1编写实属不易,点赞,留言, 查询数据翻倍完美解决!
