Mybatis中Integer类型的判断<if test=“status!= null and status!= ‘‘“>问题

在使用Mybatis进行数据库查询时,若采用<iftest=status!=nullandstatus!=''>进行判空操作,会误将status为0视为null,导致查询结果异常。本文解析了这一误区,并提供了正确的判空逻辑实现方式。

Mybatis在进行<if test="status!= null and status!= ''">判空操作时,如果status为0的时候,该判断条件的值为false,也就是说Mybatis此时把0作为null来进行判断的

此时就会出现问题,在查询状态是0的数据时,查询的是全部数据

 

解决办法:

将判断条件修改为:<if test="status!= null">

结论:

  1. <if test="status != null">中status为integer类型的,status=0的判断结果为true。
  2. <if test="status != null and status != ''">中status为integer类型的,status=0的判断结果为false,mybatis把status看作string来进行判断。
为以下代码在右侧生成注释<?xml version="1.0" encoding="UTF-8" ?> <!-- sky-server\src\main\resources\mapper\DishMapper.xml --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sky.mapper.DishMapper"> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user,status) values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser},#{status}) </insert> <select id="pageQuery" resultType="com.sky.vo.DishVO"> select d.*,c.name as categoryName from dish d left outer join category c on d.category_id = c.id <where> <if test="name != null"> and d.name like concat('%',#{name},'%') </if> <if test="categoryId != null"> and d.category_id = #{categoryId} </if> <if test="status != null"> and d.status = #{status} </if> </where> order by d.create_time desc </select> <select id="list" resultType="com.sky.entity.Dish"> select * from dish <where> <if test="categoryId != null"> and category_id = #{categoryId} </if> <if test="name != null"> and name = #{name} </if> <if test="status != null"> and status = #{status} </if> </where> order by create_time desc </select> <select id="countByMap" resultType="java.lang.Integer"> select count(id) from dish <where> <if test="status != null"> and status = #{status} </if> <if test="categoryId != null"> and category_id = #{categoryId} </if> </where> </select> <update id="update"> update dish <set> <if test="name != null"> name = #{name}, </if> <if test="categoryId != null"> category_id = #{categoryId}, </if> <if test="price != null"> price = #{price}, </if> <if test="image != null"> image = #{image}, </if> <if test="description != null"> description = #{description}, </if> <if test="status != null"> status = #{status}, </if> <if test="updateTime != null"> update_time = #{updateTime}, </if> <if test="updateUser != null"> update_user = #{updateUser}, </if> </set> where id = #{id} </update> </mapper>
12-13
为以下代码在右侧生成注释<?xml version="1.0" encoding="UTF-8" ?> <!-- sky-server\src\main\resources\mapper\setmealMapper.xml --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sky.mapper.SetmealMapper"> <insert id="insert" parameterType="com.sky.entity.Setmeal" useGeneratedKeys="true" keyProperty="id"> insert into setmeal(category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) values (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}) </insert> <update id="update"> update setmeal <set> <if test="categoryId != null"> category_id = #{categoryId}, </if> <if test="name != null"> name = #{name}, </if> <if test="price != null"> price = #{price}, </if> <if test="status != null"> status = #{status}, </if> <if test="description != null"> description = #{description}, </if> <if test="image != null"> image = #{image}, </if> <if test="createTime != null"> create_time = #{createTime}, </if> <if test="updateTime != null"> update_time = #{updateTime}, </if> <if test="createUser != null"> create_user = #{createUser}, </if> <if test="updateUser != null"> update_user = #{updateUser}, </if> </set> where id = #{id} </update> <select id="pageQuery" resultType="com.sky.vo.SetmealVO"> select s.*,c.name categoryName from setmeal s left join category c on s.category_id = c.id <where> <if test="name != null"> and s.name like concat('%',#{name},'%') </if> <if test="status != null"> and s.status = #{status} </if> <if test="categoryId != null"> and s.category_id = #{categoryId} </if> </where> order by s.create_time desc </select> <select id="list" parameterType="com.sky.entity.Setmeal" resultType="com.sky.entity.Setmeal"> select * from setmeal <where> <if test="name != null"> and name like concat('%',#{name},'%') </if> <if test="categoryId != null"> and category_id = #{categoryId} </if> <if test="status != null"> and status = #{status} </if> </where> </select> <select id="countByMap" resultType="java.lang.Integer"> select count(id) from setmeal <where> <if test="status != null"> and status = #{status} </if> <if test="categoryId != null"> and category_id = #{categoryId} </if> </where> </select> </mapper>
最新发布
12-13
为以下代码在右侧生成注释<?xml version="1.0" encoding="UTF-8" ?> <!-- sky-server\src\main\resources\mapper\OrderMapper.xml --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sky.mapper.OrderMapper"> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod}, #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{userName}, #{consignee}, #{cancelReason}, #{rejectionReason}, #{cancelTime}, #{estimatedDeliveryTime}, #{deliveryStatus}, #{deliveryTime}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus}) </insert> <update id="update" parameterType="com.sky.entity.Orders"> update orders <set> <if test="cancelReason != null and cancelReason!='' "> cancel_reason=#{cancelReason}, </if> <if test="rejectionReason != null and rejectionReason!='' "> rejection_reason=#{rejectionReason}, </if> <if test="cancelTime != null"> cancel_time=#{cancelTime}, </if> <if test="payStatus != null"> pay_status=#{payStatus}, </if> <if test="payMethod != null"> pay_method=#{payMethod}, </if> <if test="checkoutTime != null"> checkout_time=#{checkoutTime}, </if> <if test="status != null"> status = #{status}, </if> <if test="deliveryTime != null"> delivery_time = #{deliveryTime} </if> </set> where id = #{id} </update> <select id="pageQuery" resultType="com.sky.entity.Orders"> select * from orders <where> <if test="number != null and number!=''"> and number like concat('%',#{number},'%') </if> <if test="phone != null and phone!=''"> and phone like concat('%',#{phone},'%') </if> <if test="userId != null"> and user_id = #{userId} </if> <if test="status != null"> and status = #{status} </if> <if test="beginTime != null"> and begin_time = #{beginTime} </if> <if test="endTime != null"> and end_time = #{endTime} </if> </where> order by order_time desc </select> <select id="sumByMap" resultType="java.lang.Double"> select sum(amount) from orders <where> <if test="begin != null"> and order_time > #{begin} </if> <if test="end != null"> and order_time < #{end} </if> <if test="status != null"> and status = #{status} </if> </where> </select> <select id="getOrderCount" resultType="java.lang.Integer"> select count(id) from orders <where> <if test="begin != null"> and order_time > #{begin} </if> <if test="end != null"> and order_time < #{end} </if> <if test="status != null"> and status = #{status} </if> </where> </select> <select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO"> select od.name,sum(od.number) number from order_detail od,orders o where od.order_id = o.id and o.status = 5 <if test="begin != null"> and o.order_time > #{begin} </if> <if test="end != null"> and o.order_time < #{end} </if> group by od.name order by number desc limit 0,10 </select> </mapper>
12-13
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值