Mybatis一对多的写法

实体类


public class AntennaSystem implements Serializable {
	private static final long serialVersionUID = 1L;
	
	/**
	* 系统id
	*/
    private String systemId;
	
	/**
	* 系统名称
	*/
    private String systemName;
	
	/**
	* 部门id
	*/
	private String departId;

	/**
	 * 部门
	 */
    private Department department;
	
	/**
	* 站主键id
	*/
	private String stationId;

	/**
	 * 站
	 */
    private Station station;
	/**
	 * 扩展属性
	 */
	private List<ExtendedAttr> extendedAttrs;

	/**
	 * 分系统
	 */
	private List<Subsystem> subsystems;
}

Mapper.xml


    <!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="com.industry.txsp.entity.AntennaSystem" id="baseAntennaSystemMap">
        <result property="systemId" column="system_id"/>
        <result property="systemName" column="system_name"/>
        <result property="departId" column="depart_id"/>
        <result property="stationId" column="station_id"/>
        <result property="completeDate" column="complete_date"/>
        <result property="deliveryDate" column="delivery_date"/>
        <result property="qaDeadline" column="qa_deadline"/>
        <result property="systemNumber" column="system_number"/>
        <result property="remark" column="remark"/>
        <result property="createId" column="create_id"/>
        <result property="creator" column="creator"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="isDeleted" column="is_deleted"/>
    </resultMap>
    
    <!--分开来写有利于解耦,更灵活-->
    <!--extends:可以继承一个已有的resultMap,指定resultMap的唯一标识(id)即可,但是在继承时,只能继承type类型一样的resultMap-->
    <resultMap extends="baseAntennaSystemMap" type="com.industry.txsp.entity.AntennaSystem" id="antennaSystemMap">
        <association property="department" resultMap="com.industry.txsp.mapper.DepartmentMapper.departmentMap"/>
        <association property="station" resultMap="com.industry.txsp.mapper.StationMapper.stationMap"/>
    </resultMap>

     
    <resultMap extends="antennaSystemMap" type="com.industry.txsp.entity.AntennaSystem" id="allAntennaSystemMap">
        <collection property="subsystems" resultMap="com.industry.txsp.mapper.SubsystemMapper.subsystemMap"/>
        <collection property="extendedAttrs" resultMap="com.industry.txsp.mapper.ExtendedAttrMapper.extendedAttrMap"/>
    </resultMap>
原文地址:https://www.cnblogs.com/junzifeng/p/12736313.html