前言:一直很想写一篇记录mybatis传值篇,我们都知道有map传参,DTO传参,list传参,string传参等等,所以我想总结一下,记录一下,其中还会讲解到#传参和$传参

一:传参类型

String 传参

mapper接口

List<BasLocation> selectInLocListForTransfer(@Param("binCode") String binCode);

xml

<select id="selectInLocListForTransfer" resultType="com.xinghuo.service.wms.entity.BasLocation">
        select loc_name, loc_code from bas_location where bin_code = #{binCode, jdbcType=VARCHAR} and is_history = '0'
</select>

DTO 传参

mapper接口

List<BasLocationDTO> getWhName(BasLocationDTO basLocationDTO);

xml

<select id="getWhName" parameterType="com.xinghuo.service.wms.data.dto.BasLocationDTO"
            resultType="com.xinghuo.service.wms.data.dto.BasLocationDTO">
        select wh_code, wh_name, factory_code from bas_warehouse where is_history=0
        <if test="factoryCode != null and factoryCode != '' ">
            and factory_code = #{factoryCode, jdbcType=VARCHAR}
        </if>
</select>

List 传参

这里有一个注解@Param,所以xml中接收的也是list
mapper接口

List<InventoryLocationDetail> selectTransferNoMixLocList(@Param("list") List<String> locCodeList);

xml

<select id="selectTransferNoMixLocList" resultType="com.xinghuo.service.wms.entity.InventoryLocationDetail">
        select b.loc_code, i.material_num from bas_location b left join inventory_location_detail i on b.loc_code =
        i.loc_code
        where b.loc_code in (<foreach collection="list" item="item" separator=",">#{item}</foreach>) and b.is_mix = 0
</select>

List<实体> 传参

mapper接口

List<BasLocation> selectRemainWeightAndVolume(@Param("list") List<InventoryLocationDetail> list);

xml

<select id="selectRemainWeightAndVolume" resultType="com.xinghuo.service.wms.entity.BasLocation">
        select l.bin_code, l.loc_code, l.loc_weight - ifnull(i.weight, 0) as loc_weight, l.loc_volume - ifnull(i.volume,
        0) as loc_volume
        from bas_location l left join (select i.bin_code,i.loc_code,sum(i.weight) weight,sum(i.volume) volume
        from inventory_location_detail i group by i.bin_code,i.loc_code ) i on l.bin_code = i.bin_code and l.loc_code =
        i.loc_code
        where (l.bin_code, l.loc_code) in (<foreach collection="list" item="item" separator=",">(#{item.binCode},
        #{item.locCode})
    </foreach> )
    </select>

map传参

注意:这里传参xml终有一个指定类型:parameterType
mapper

 List<Advertise> selectBlackAdvertiseList(@Param("params") Map<String, Object> map);

xml


<select id="selectBlackAdvertiseList" parameterType="java.util.Map" resultMap="BaseResultMap">
        SELECT * FROM bfc_advertise
        <where>
            1 = 1
            <if test="params.startTime != null and params.startTime != ''">
                and put_start_time LIKE CONCAT('%',#{params.startTime},'%')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and put_end_time LIKE CONCAT('%',#{params.endTime},'%')
            </if>
            LIMIT #{params.pageNo},#{params.pageSize}
        </where>
</select>

二:$ 和 # 意义