MyBatis中<if>内对数值类型的判断不通过

【背景】实体类中定义了 Integer类型的status属性,取值有 0(停用)和 1(启用)

【问题】查询停用状态(status =0)数据时,查询出了所有数据,xml文件中的内容如下:

<if test="status !=null and status != ''">
    AND Status = #{status}
</if> 

【原因】MyBatis默认了 Integer类型数据值等于0时为 ""(空字符串)。

要查询全部数据时status值为null,查询启用/停用数据时值为1/0,没有必要判断是否为空字符串,因此修改后的代码如下:

<if test="status != null">
    AND Status = #{status}
</if> 

note:实体类要写数值类型要用 Integer 而不是int。开发中状态的取值往往为0或1,如果为int 类型,那么Java为该属性赋的默认值为0,分不清是前端传递的0还是Java赋予的默认值。

解释一下列语句的语法,纯文本输出.<?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>
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值