4.Mybatis中,在Mapper的SQL映射文件中,使用<choose><when>无法识别参数的情况

正确结果

xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.BrandMapper">


    <!-- 完成数据库表和Java代码的映射   -->
    <resultMap id="BrandResultMap" type="brand">
        <result column="brand_name" property="brandName"></result>
        <result column="company_name" property="companyName"></result>
    </resultMap>


    <select id="selectByConditionSingle" resultMap="BrandResultMap">
        select *
        from tb_brand
        where
        <choose><!--相当于switch-->
            <when test="status != null"><!--相当于case-->
                status = #{status}
            </when>
            <when test="companyName != null and companyName != '' "><!--相当于case-->
                company_name like #{companyName}
            </when>
            <when test="brandName != null and brandName != ''"><!--相当于case-->
                brand_name like #{brandName}
            </when>
            <otherwise>
                1 = 1
            </otherwise>
        </choose>

    </select>
</mapper>

由于使用Brand对象传参,此时使用标签,没有给Brand对象赋值,此时应该查询数据库的三条记录

但是,如果因为实体类的变量数据类型为基本类型,而不是它的包装类型,可能会出现如下错误

此时,status的默认值就会变为0,达不到预期的效果

下方为正确的实体类代码

java 复制代码
package com.itheima.pojo;

/**
 * @package: com.itheima.pojo
 * @className: Brand
 * @Description:
 * @author: haozihua
 * @date: 2024/11/8 10:32
 */
public class Brand {
    private Integer id;
    private String brandName;
    private String companyName;
    private Integer ordered;
    private String description;
    private Integer status;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}
相关推荐
乌暮1 天前
JavaEE初阶--多线程案例
java·java-ee
小光学长1 天前
基于ssm旅游管理系统的开发与设计z050cft7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·旅游
爱丽_1 天前
MyBatis动态SQL完全指南
服务器·sql·mybatis
用户3521802454751 天前
🥯2025 年终极避坑指南:Spring Boot 2.7 + 3.2 混合集群的 Redis + OAuth2 序列化血泪史
java·后端·spring cloud
陌路物是人非1 天前
记一个 @Resource BUG
java·开发语言·bug
晴天飛 雪1 天前
Spring Boot 上传shp压缩包解析多少地块
java·spring boot
曹牧1 天前
Java:Math.abs()‌
java·开发语言·算法
期待のcode1 天前
Java的泛型
java·开发语言
FPGAI1 天前
Java学习之计算机存储规则、数据类型、标识符、键盘录入、IDEA
java·学习
AC赳赳老秦1 天前
pbootcms模板后台版权如何修改
java·开发语言·spring boot·postgresql·测试用例·pbootcms·建站