springboot-mybatis常用方法整理(like+转义+批量新增sql组装)

1.like

 <select id="selectByCondition"  resultMap="BaseResultMap">
    <if test=" supplierName != null "><bind name="namecnd" value="'%' + _parameter.supplierName + '%'"  /></if>
    <if test=" address != null "><bind name="addresscnd" value="'%' + _parameter.address + '%'"  /></if>
    select
    <include refid="Base_Column_List" />
    from onecity_supplier
    where isdelete =0
    <if test=" supplierName != null "> and supplier_name like #{namecnd} </if>
    <if test=" address != null "> and address like #{addresscnd} </if>
    <if test=" creator != null "> and creator = #{creator} </if>
    <if test=" phone != null "> and phone = #{phone} </if>
  </select>

2.符号转义

 &lt;   <
 &gt; >  
 &amp; &
 &apos;'
 &quot;"

3. 示例:转义+ <where>+  foreach

 <select id="selectByCondition"  resultType="hashmap" >
    select
   updatetime
    from devices.device_state_newest 
    <where>
      <if test="deviceStatus !=null">  and  device_status = #{deviceStatus} </if>
      <if test="starttime !=null">   AND createtime &gt;= #{starttime} </if>
      and  org_id IN
      <foreach item="item" collection="orgids" separator="," open="(" close=")" index="index">
      #{item}
      </foreach>
    </where>
  </select>

4. 示例:批量添加sql组装

 </insert>
    <insert id="batchinsert" >
    insert into onecity_user_org ( org_id, user_id,
      relation, enable, username
      )
    values
        <foreach item="item" collection="users" separator="," open="" close="" index="index">
            ( #{item.orgId,jdbcType=VARCHAR}, #{item.userId,jdbcType=VARCHAR},
            #{item.relation,jdbcType=INTEGER}, #{item.enable,jdbcType=INTEGER},  #{item.username,jdbcType=VARCHAR}
            )
        </foreach>

  </insert>

5. 字段值为null的属性正常返回

properties添加配置

mybatis.configuration.call-setters-on-nulls=true

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值