代码改变世界

mapper.xml中 一些解释

2019-08-30 18:17  握瑾怀瑜  阅读(574)  评论(0)    收藏  举报

1:resultMap解释与使用

  在mybatis中有一个resultMap标签,它是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与数据库表中的字段进行关联映射

  注意

  当实体类中的字段与数据库表中的字段相同时,可以将resultMap标签中的关联关系忽略不写。 
  当实体类中的字段与数据库表中的字段不相同时,就需要在resultMap标签中将实体类字段与数据库字段一 一进行关联映射,或者开启驼峰规则,让它自动转换

 2: resultType 与 resultMap不能同时使用

  参考文献:https://blog.csdn.net/paincupid/article/details/50616722

报错

 单独使用  无错

<!-- 返回满足条件的设备的数量 -->
    <select id="getDevicesCountByParams"  resultType="java.lang.Integer">
        SELECT count(*) FROM wg_devices where user_id in
        <foreach item="userIdList" index="index" collection="userIdList" open="(" separator="," close=")">
            #{userIdList}
        </foreach>
        <if test="deptId != ''">
            <if test="deptId != '未知'.toString()">
                and dept_id=#{deptId}
            </if>
            <if test="deptId == '未知'.toString()">
                and dept_id = ''
            </if>
        </if>
        <if test="onlineStatus != '' and onlineStatus == '在线'.toString()">
            and TIMESTAMPDIFF(SECOND ,update_at,DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')) &lt;=remark1 and ip1&lt;&gt; ''
        </if>
        <if test="onlineStatus != '' and onlineStatus == '离线'.toString()">
            and TIMESTAMPDIFF(SECOND,update_at,DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')) &gt;remark1 and ip1&lt;&gt; ''
        </if>
        <if test="onlineStatus != '' and onlineStatus == '未注册'.toString()">
            and ip= ''
        </if>
        <if test="onlineStatus != '' and onlineStatus == '未录入'.toString()">
            and another_name= ''
        </if>
        <if test="operator1 != ''">
            <if test="operator1 != '未知'.toString()">
                and operator1 like CONCAT('%',#{operator1},'%')
            </if>
            <if test="operator1 == '未知'.toString()">
                and operator1 = ''
            </if>
        </if>
        <if test="businessDeviceModel != ''">
            <if test="businessDeviceModel != '未知'.toString()">
                and business_device_model like CONCAT('%',#{businessDeviceModel},'%')
            </if>
            <if test="businessDeviceModel == '未知'.toString()">
                and business_device_model = ''
            </if>
        </if>
        <if test="ip1 != ''">
            <if test="ip1 != '未知'.toString()">
                and ip1 like CONCAT('%',#{ip1},'%')
            </if>
            <if test="ip1 == '未知'.toString()">
                and ip1 = ''
            </if>
        </if>
        <if test="alarmInfo != ''">
            <if test="alarmInfo != '未知'.toString()">
                and alarm_info like CONCAT('%',#{alarmInfo},'%')
            </if>
            <if test="alarmInfo == '未知'.toString()">
                and alarm_info = ''
            </if>
        </if>
        <if test="venderName != ''">
            <if test="venderName != '未知'.toString()">
                and vender_name like CONCAT('%',#{venderName},'%')
            </if>
            <if test="venderName == '未知'.toString()">
                and vender_name = ''
            </if>
        </if>
        <if test="anotherName != ''">
            <if test="anotherName != '未知'.toString()">
                and another_name like CONCAT('%',#{anotherName},'%')
            </if>
            <if test="anotherName == '未知'.toString()">
                and another_name = ''
            </if>
        </if>
        <if test="groupId != ''">
            <if test="groupId != '未知'.toString()">
                and group_id=#{groupId}
            </if>
            <if test="groupId == '未知'.toString()">
                and group_id = ''
            </if>
        </if>
        <if test="installPlace != ''">
            <if test="installPlace != '未知'.toString()">
                and install_place=#{installPlace}
            </if>
            <if test="installPlace == '未知'.toString()">
                and install_place = ''
            </if>
        </if>
        <if test="area != ''">
            <if test="area != '未知'.toString()">
                and area=#{area}
            </if>
            <if test="area == '未知'.toString()">
                and area = ''
            </if>
        </if>
        <if test="networkType != '' and networkType == '2'.toString()">
            and (network_type1 = 'EDGE' OR  network_type1 = 'CDMA')
        </if>
        <if test="networkType != '' and networkType == '3'.toString()">
            and (network_type1 = 'WCDMA' OR  network_type1 = 'TD-SCDMA' OR  network_type1 = 'CDMA2000' OR  network_type1 = 'HSPA+')
        </if>
        <if test="networkType != '' and networkType == '4'.toString()">
            and (network_type1 = 'FDD-LTE' OR  network_type1 = 'TDD-LTE' OR  network_type1 = 'LTE-1.8GHz' OR  network_type1 = 'LTE-230MHz')
        </if>
        <if test="networkType != '' and networkType == '未知'.toString()">
            and network_type1 = ''
        </if>

        <if test="signal != '' and signal == '0'.toString()">
            and (rssi1&lt;=-130)
        </if>
        <if test="signal != '' and signal == '1'.toString()">
            and (rssi1&gt;-130 and rssi1&lt;=-100)
        </if>
        <if test="signal != '' and signal == '2'.toString()">
            and (rssi1&gt;=-99 and rssi1&lt;=-90)
        </if>
        <if test="signal != '' and signal == '3'.toString()">
            and (rssi1&gt;=-89 and rssi1&lt;=-70)
        </if>
        <if test="signal != '' and signal == '4'.toString()">
            and rssi1&gt;=-69
        </if>

        and is_delete &lt;&gt; '1'
        <if test="flowOrder != '' and flowOrder == 'max'.toString()">
            order by  wireless_flow1*1 desc
        </if>
        <if test="flowOrder != '' and flowOrder == 'min'.toString()">
            order by  wireless_flow1*1 ASC
        </if>
        <if test="flowOrder == ''">
            order by  update_at desc
        </if>
    </select>
View Code