MyBatis parameter from HashMap

2019-01-28 13:53发布

问题:

In mapper interface I have:

ArrayList<Item> select(@Param("filterId")int filterId, @Param("filterData")HashMap<String,Object> filterData);

In mapper xml I have:

 <select id="select" parameterType="map" resultMap="RM">
        SELECT ... 
        FROM ....
        WHERE id=#{filterData["id"]}
    </select>

No errors but the result is not as expected (it returns empty set but I know item with such id exists). The #{filterData["id"]} seems not to work. Where is my mistake?

回答1:

I found the answer:

 <select id="select" parameterType="map" resultMap="RM">
        SELECT ... 
        FROM ....
        WHERE id=#{filterData.id}
    </select>


回答2:

If you have pure java class you can map in the parameterType as input to the query and return as different custom pojo class like this.

<select id="getCustomMember" parameterType="com.custom.member" resultMap="custDocMap">
        select
        customer_id, cust_start_dt, cust_type, src_sys_doc_id, 
        legal_backer_id, eenv_create_dt
        from
        web_data..wd_edoc
        where
        eenv_create_dt = #{member.date}
    </select>