mybatis xml 对int类型如何写if条件避免java的默认值0的干扰
在 MyBatis 中,如果你想在 XML 配置中使用
<if>
标签来判断
int
类型的参数,并且避免 Java 的默认值
0
干扰你的判断,你可以使用
Integer
包装类而不是基本类型
int
。这是因为
Integer
可以为
null
,而基本类型
int
不能。
示例表结构
假设有一个名为 work_orders
的表:
CREATE TABLE work_orders (
id INT AUTO_INCREMENT PRIMARY KEY,
order_status INT,
order_time DATETIME
);
使用 Integer 类型参数
1. Mapper 接口
首先,在你的 Mapper 接口中使用 Integer
类型:
package com.example.mapper;
import com.example.model.WorkOrder;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface WorkOrderMapper {
List<WorkOrder> selectWorkOrders(@Param("orderStatus") Integer