zoukankan      html  css  js  c++  java
  • SSM框架 mapper.xml中 value的空值判断问题

    先看解决方案,其他的都是问题的出处

    解决方案:if中使用 _parameter,#{value}不变

    <if test="_parameter!='' and _parameter!=null">
          join scm_product p on pt.ProductTypeID=p.ProductTypeID
          where (p.ProductNameCN like concat('%',#{value},'%')
          or p.ProductNameEN like concat('%',#{value},'%'))
        </if>

    问题由来:

    由于最近项目转型,使用 java开发
    使用ssm框架时,遇到一个问题。(java中,我也只能自认菜鸡了

    问题:

    先看一下数据库。


    然后看看 mapper.xml

    Java code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <select id="myTest" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        *
        from scm_producttype pt
        <if test='value!="" and value!=null'>
          where TypeNameCN='test'
        </if>
        order by pt.ProductTypeID
      </select>



    很明显,我在myTest方法中增加了一个if标签。这里只是例子,判断了value不为""和null

    最后我在测试中,调试信息为:


    很奇怪,
    当传值null,那么可以查询出2条数据。(说明mapper.xml中if跳过了)
    当传值"",却只能查询1条数据。(说明mapper.xml中if没有跳过

    解决方案:

    <if test="_parameter!='' and _parameter!=null">
          join scm_product p on pt.ProductTypeID=p.ProductTypeID
          where (p.ProductNameCN like concat('%',#{value},'%')
          or p.ProductNameEN like concat('%',#{value},'%'))
        </if>
  • 相关阅读:
    【codevs1515】 跳
    【bzoj1227】 SDOI2009—虔诚的墓主人
    【bzoj3505】 Cqoi2014—数三角形
    【bzoj1059】 ZJOI2007—矩阵游戏
    【poj2122】 Optimal Milking
    【poj2455】 Secret Milking Machine
    【poj3084】 Panic Room
    【poj2699】 The Maximum Number of Strong Kings
    【bzoj3218】 a + b Problem
    【bzoj3122】 Sdoi2013—随机数生成器
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779803.html
Copyright © 2011-2022 走看看