mybatis编写sql条件值为0时无效

今天做项目发现当传回sql条件为0时,后台判断无效

<el-form-item prop="state">
      <el-select v-model="filters.state" placeholder="客户状态" value="">
          <el-option
              v-for="item in states"
               :key="item.id"
               :label="item.name"
                :value="item.id">
                </el-option>
       </el-select>
 </el-form-item>

模拟数据

states:[{
                        id:,
                        name:
                },{
                    id: 0,
                    name: 正式客户
                }, {
                    id: 1,
                    name: 潜在客户
                }, {
                    id: 2,
                    name: 资源客户
                }],

后台sql

<if test="state != null and state != ">
   and c.customerstate=#{state}
 </if>

上面的条件判断了空与空串,在mybatis中传入值为int时,判断空串时会将0值转换成空串导致条件失效; 只需去掉空串判断

<if test="state != null">
    and c.customerstate=#{state}
</if>
经验分享 程序员 微信小程序 职场和发展