mybatis 一对多查询
Mappe文件
<resultMap type="BwProject" id="BwProjectResult"> <id property="id" column="id" /> <result property="projectCode" column="PROJECT_CODE" /> <result property="projectName" column="PROJECT_NAME" /> <result property="status" column="STATUS" /> <result property="voltage" column="VOLTAGE" /> <result property="constructionUnit" column="CONSTRUCTION_UNIT" /> <result property="contractAmount" column="CONTRACT_AMOUNT" /> <result property="researchReplyTime" column="RESEARCH_REPLY_TIME" /> <result property="desiginReplyTime" column="DESIGIN_REPLY_TIME" /> <result property="budgetReplyTime" column="BUDGET_REPLY_TIME" /> <result property="materialBiddTime" column="MATERIAL_BIDD_TIME" /> <result property="compleOperateTime" column="COMPLE_OPERATE_TIME" /> <result property="createPeople" column="CREATE_PEOPLE" /> <result property="createTime" column="CREATE_TIME" /> <result property="updatePeople" column="UPDATE_PEOPLE" /> <result property="updateTime" column="UPDATE_TIME" /> <collection property="children" ofType="com.booway.project.domain.BwEngineering" column="pid"> <id property="id" column="eid" /> <result property="pid" column="pid" /> <result property="engineerCode" column="ENGINEER_CODE" /> <result property="engineerName" column="ENGINEER_NAME" /> </collection> </resultMap> <select id="selectLyPro" parameterType="BwProject" resultMap="BwProjectResult"> select a.*,b.ENGINEER_CODE,b.id as eid,b.pid from bw_project a LEFT JOIN bw_engineering b on a.id=b.pid </select>
实体类
/** * 主键 */ @TableId(value = "ID", type = IdType.AUTO) private Long id; /** * 项目编码 */ @TableField(value = "PROJECT_CODE") private String projectCode; /** * 项目名称 */ @TableField(value = "PROJECT_NAME") private String projectName; /** * 状态 */ @TableField(value = "STATUS") private String status; /** * 电压表 */ @TableField(value = "VOLTAGE") private String voltage; /** * 建管单位 */ @TableField(value = "CONSTRUCTION_UNIT") private String constructionUnit; /** * 合同款 */ @TableField(value = "CONTRACT_AMOUNT") private BigDecimal contractAmount; /** * 可研批复时间 */ @TableField(value = "RESEARCH_REPLY_TIME") private Date researchReplyTime; /** * 初设批复时间 */ @TableField(value = "DESIGIN_REPLY_TIME") private Date desiginReplyTime; /** * 施工图预算批复时间 */ @TableField(value = "BUDGET_REPLY_TIME") private Date budgetReplyTime; /** * 物资招标时间 */ @TableField(value = "MATERIAL_BIDD_TIME") private Date materialBiddTime; /** * 竣工投产时间 */ @TableField(value = "COMPLE_OPERATE_TIME") private Date compleOperateTime; /** * 创建者 */ @TableField(value = "CREATE_PEOPLE") private String createPeople; /** * 创建时间 */ @TableField(value = "CREATE_TIME") private Timestamp createTime; /** * 更新者 */ @TableField(value = "UPDATE_PEOPLE") private String updatePeople; /** * 更新时间 */ @TableField(value = "UPDATE_TIME") private Timestamp updateTime; /** * 虚拟主键 */ @TableField(exist = false) private String mxVirtualId; // 关联工程表 private List<BwEngineering> children;
这里会有个问题 因为bw_project 有字段id bw_engineering也有字段id
查询的时候 一个bw_project 对应的是多个 bw_engineering 但是只会显示一个 bw_engineering
那是因为当两个表有相同的字段的时候必须使用别名 这里我把 bw_engineering表的 id 改为了 eid
此时查询出来的结果就是对的