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>

 

posted @ 2017-06-07 14:24  正怒月神  阅读(1506)  评论(0编辑  收藏  举报