<select id="getCommodityMany" parameterType="commodityVO" resultType="commodityVO" >
select C_NUMBER as cnumber,
C_NAME as cname,
C_TYPE as ctype,
C_DESCRIPTION as cdescription,
C_COUNT as ccount
from commodity c
<where>
<if test="#{cnumber} != null and #{cnumber}!=''">
c.C_NUMBER=#{cnumber}
</if>
<if test="#{cname} != null and #{cname}!=''">
and c.C_NAME like #{cname}
</if>
<if test="#{ctype} != null and #{ctype}!=''">
and c.C_TYPE = #{ctype}
</if>
</where>
</select>
本人目的是为了实现多条件查询 但是必须把这3个条件全部输入的才能查出值 少一个都不能查出值
<select id="getCommodityMany" parameterType="commodityVO" resultType="commodityVO" >
select C_NUMBER as cnumber,
C_NAME as cname,
C_TYPE as ctype,
C_DESCRIPTION as cdescription,
C_COUNT as ccount
from commodity c
改成这样 值能全部查询出来 问题好像出现在 if语句 个人感觉没有错误啊!
望高人解决
------解决方案--------------------
<if test="#{cnumber} != null and #{cnumber}!=''">
=>
<if test="cnumber != null and cnumber!=''">
------解决方案--------------------
既然你必须要三个值都存在才能查出东西 那你能确认自己传进来的三个参数都有值? 你可以吧sql打印在控制台或者日志文件看看。
还有一点:
<where>
<if test="#{cnumber} != null and #{cnumber}!=''">
c.C_NUMBER=#{cnumber}
</if>
<if test="#{cname} != null and #{cname}!=''">
and c.C_NAME like #{cname}
</if>
<if test="#{ctype} != null and #{ctype}!=''">
and c.C_TYPE = #{ctype}
</if>
</where>
这条sql 你可以改成这样的
where
1=1
<if test="#{cnumber} != null and #{cnumber}!=''">
and c.C_NUMBER=#{cnumber}
</if>
<if test="#{cname} != null and #{cname}!=''">
and c.C_NAME like #{cname}
</if>
<if test="#{ctype} != null and #{ctype}!=''">
and c.C_TYPE = #{ctype}
</if>