mybatis 一對多查詢 帶條件

上級實體類(一)

public class DefenceAreaEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;

    /**
     * 名稱
     */
    private String name;

    /**
     * 集合
     */
    private List<DeploymentTimeEntity> dteList;
}

下級實體類(多)

public class DeploymentTimeEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 關聯id
     */
    private Integer defenceAreaId;
   
    /**
     * 類型1:工作日2:節假日
     */
    private Integer type;

}

mapper

返回使用的resultMap 對應關係使用collection。

<resultMap id="BaseResultMap" type="com.sinopec.web.entity.DefenceAreaEntity">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <collection property="dteList" column="{defenceAreaId = id,type = type}" javaType="ArrayList" ofType="com.sinopec.web.entity.DeploymentTimeEntity" select="findDteByDaId">
            <id column="id" property="id" />
            <id column="defence_area_id" property="defenceAreaId" />
            <result column="type" property="type" />
        </collection>
    </resultMap>

<!-- 列表查詢 -->
    <select id="findListByMap" resultMap="BaseResultMap" parameterType="hashmap">
        select id, name, #{type} type from defence_area
            where del_flag = 0
    </select>
    <select id="findDteByDaId" parameterType="hashmap" resultType="com.sinopec.web.entity.DeploymentTimeEntity">
        select id, defence_area_id, type from deployment_time
        where defence_area_id = #{defenceAreaId} and type = #{type}
    </select>

collection具體配置講解

    property:實體類(一)中的名稱

    column:一對多關聯查詢需要使用的條件值,

                以上圖例子來說defenceAreaId、type爲子查詢需要使用的字段名字(前臺傳來的搜索數據值)

                id、type爲父查詢中返回的字段名(數據庫的列名)。

                通俗就是column就是把父查詢返回的值給傳遞到子查詢中,具體實現就是拿父查詢的id列去對應子查詢的條件值

    ofType:子查詢返回的數據類型,類似於resultType

    select:子查詢的名字,同一mapper下無需指定具體的路徑,非同一mapper則需指定的com.xxxx.xxx

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章