mybatis中批量操作时判断参数是否为null和空 mapper.xml配置

第一种:使用where标签

<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t
<where>
<if test=" 传进来的字段 != null and 传进来的字段 != ''">
and t.字段 like '%${传进来的字段}%'
</if>
<if test="传进来的字段 != null and 传进来的字段 != ''">
and t.ASSET_TYPE like '%${assetType}%'
</if>
</where>
</select>

Mybatis中where 标签知道只有在一个以上的if条件有值的情况下才去插入“where”子句,若最后的内容是“and”或“or”开头的,where 标签会知道如何将他们去除

第二种:使用trim标签

<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t

<trim prefix="where" prefixOverrides="and|or">

<if test=" 传进来的字段 != null and 传进来的字段 != ''">
and t.字段 like '%${传进来的字段}%'
</if>

<if test="传进来的字段 != null and 传进来的字段 != ''">
and t.ASSET_TYPE like '%${assetType}%'
</if>
</trim>
</select>

Mybatis中trim是更灵活的去处多余关键字的标签,他可以实现where的效果

1、新增

出入一个listlist里面可以为bean,也可以为map类型,判断的时候区分大小写;此处判断了每个字段是否为空
注意datetimestamp类型,不能进行item.dealdate !=''此种判断

<insert id="insertBatch" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator="union all" >  
        insert into POS_CHECK_DATA(
            <if test=" item.businessfrom != null and item.businessfrom !=''"> businessfrom, </if>    
            <if test=" item.dealdate != null and item.dealdate !=''"> dealdate, </if>
            <if test=" item.dealtime != null and item.dealtime !=''"> dealtime, </if>    
            <if test=" item.expno != null and item.expno !=''"> expno, </if>
            <if test=" item.exptype != null and item.exptype !=''"> exptype, </if>    
            <if test=" item.ordercod != null and item.ordercod !=''"> ordercod, </if>    
            <if test=" item.goodcod != null and item.goodcod !=''"> goodcod, </if>
            <if test=" item.expcod != null and item.expcod !=''"> expcod, </if>
            <if test=" item.paytype != null and item.paytype !=''"> paytype, </if>
            <if test=" item.countdate != null and item.countdate !=''"> countdate, </if>
            
            
            <if test=" item.countcod != null and item.countcod !=''"> countcod, </if>
            <if test=" item.cardfee != null and item.cardfee !=''"> cardfee, </if>
            <if test=" item.cardno != null and item.cardno !=''"> car
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值