Mybatis(动态sql)

这里写了一个带条件的动态模糊查询,大家首先联想的是不是当这个查询为空得判断是否为空?用if标签来判断

select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id

      where 1=1

<if test="qname!=' '  ">    注意:这里不能写为null

and     a.productname  like  '%${qname}%'

</if>

<if   test="qid != '请选择'">   注意:这里的请选择指的JSP动态页面中下拉框的选项 value="请选择"

and     b.proname=#{qid}

</if>

<if test="ispay !='请选择' ">

and  a.ispayment=#{ispay}

</if>

if标签表示判断,如果符合条件,则执行条件内容

这里的qname,qid,ispay是通过接口中的方法用Hashmap集合存的  

HashMap<String, Object> map=new HashMap<String,Object>();

我们只知道键是String类型,并不知道值是什么类型。所以定义一个Object

然后servlet中存的map.put("qname", "%"+queryProductName+"%");
                    //map.put("qname", queryProductName);
                      map.put("qid", queryProviderId);
                      map.put("ispay",queryIsPayment );

然后用where+if标签代替

<select  id="getAllSmbmsBillByLike2" parameterType="java.util.HashMap" resultMap="rmap">
    select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id

 <where>     //where代替了where 1=1; 这个表结构
            <if test="qname!='' ">
              and a.productname like #{qname}       注意这里的两个%%在servlet已经完成拼接
              </if>
              <if test="qid !='请选择' ">
              and  b.proname=#{qid}
              </if>
              <if test="ispay !='请选择' ">
              and  a.ispayment=#{ispay}
              </if>
            </where>

</select>

where标签,表示条件的连接符.如果判断条件都不成立的时候,自动把where关键字去掉

如果成立的时候,会自动的把where 关键字后面的and连接符去掉

然后在利用trim标签代替

<select  id="getAllSmbmsBillByLike2" parameterType="java.util.HashMap" resultMap="rmap">
    select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id
            <trim prefix="where" prefixOverrides="and">   //trim这个标签使用场景比较广泛,设置前缀是where
             <if test="qname!=' '  ">
               a.productname like '%${qname}%'
              </if>
              <if test="qid !='请选择' ">
               and  b.proname=#{qid}
              </if>
              <if test="ispay !='请选择' ">
               and  a.ispayment=#{ispay}
              </if>
            </trim>

</select>

trim表示去掉多余的指定的字符,prefix表示前缀,suffix表示后缀,suffixOverrides去除字段之后的指定字符

      prefixOverrides去除字段之前的指定字符

set标签 ,选择性修改。

<update  id="updateSmbmsBill" parameterType="sbill">
        update smbms_bill
        <trim prefix="set" suffixOverrides=","    suffix="where id=#{id}">
            <if  test="billcode!=' '  ">
            billcode=#{billcode},
            </if>
            <if test="productname!='' ">
                   productname=#{productname},
            </if>
            <if test="productunit!='' "> 
                productunit=#{productunit},
            </if>
            <if test="productcount!='' ">
            productcount=#{productcount},
            </if>
            <if test="totalprice!='' ">
            totalprice=#{totalprice},
            </if>
            <if test="ispayment!='' ">
            ispayment=#{ispayment}
            </if>
        </trim>
    </update>

Foreach标签,适用于批量查询和批量删除  (查询与删除同理)

注意:使用foreach,接口的参数类型有两种,第一种是list集合,第二种是数组array类型。

这里使用的是第二种数组List<SmbmsRole>  getAllUserInfoByForeach(String[] str);//批量查询 

<select id="getAllUserInfoByForeach" resultMap="rmap2">
    select b.id, b.usercode, b.username, b.gender, 
          floor(months_between(sysdate,b.birthday)/12) as age,
            b.phone, a.rolename
               from smbms_role a
              inner join smbms_user b on a.id = b.userrole
              where b.userrole in       
              <!-- item:选项 这里的属性随便取,要跟#{item}属性值对应

                    注意查询的时候使用的是in关键字
                   open:开始
                   separator:分隔符
                   close:结束
                   collection:集合是数组
               -->
              <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
              #{item}
              </foreach>
    </select>

然后在使用另一种list集合

//批量查询

   public List selectProdcutByForeach(List pList);

<!-- foreach批量查询

      foreach标签表示循环标签,collection表示集合属性,属性值有两种,

      如果接口的参数是List类型,那么该属性值=list

      如果接口的参数是数组类型,那么该属性值=array

      open属性表示类似于前缀

      close表示类似于后缀

      item表示集合的遍历体,属性值随意起

      separator表示隔离间隔的关键字属性,

      sql:SELECT * FROM product WHERE id IN(47,46,1,10)

      foreach标签之间展示的每次遍历的id值,表达形式#{item属性值}

   -->

   <select id="selectProdcutByForeach" parameterType="java.util.List" resultType="product">

      select

        <include refid="basesql"/>

      from product

      <where>

        id in

        <foreach collection="list" open="(" item="aid" separator="," close=")">

           #{aid}

        </foreach>

      </where>

   </select>

 

Choose标签(不建议使用)

Choose+when+otherwise联合使用

当有一个when条件成立的时候则执行,那么之后的when条件不管成立与否均不在执行

      当所有的when条件都不成立的时候,则执行otherwise条件

<select id="selectProductBySearchdong" parameterType="product" resultType="product">

      select

        <include refid="basesql"/>

      from product

   <where>

      <choose>

        <when test="price>0">

           and price=#{price}

        </when>

        <when test="name!=null">

           or name like '%${name}%'

        </when>

        <otherwise>

           and description=#{description}

        </otherwise>

      </choose>

   </where>

   </select>

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

brid_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值