当前位置: 首页 > news >正文

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

正确结果

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//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_brandwhere<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,达不到预期的效果
下方为正确的实体类代码

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;}@Overridepublic String toString() {return "Brand{" +"id=" + id +", brandName='" + brandName + '\'' +", companyName='" + companyName + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}
}
http://www.lryc.cn/news/484250.html

相关文章:

  • antd proFromSelect 懒加载+模糊查询
  • Spring Boot 牛刀小试 org.springframework.boot:spring-boot-maven-plugin:找不到类错误
  • qt中ctrl+鼠标左键无法进入
  • 丹摩征文活动 | 丹摩智算平台:服务器虚拟化的璀璨明珠与实战秘籍
  • 本机ip地址和网络ip地址一样吗
  • websocket身份验证
  • 案例解读 | 某三甲医院IT监控体系升级实例
  • Ubuntu20.04 为脚本文件创建桌面快捷方式 ubuntu
  • LeetCode297.二叉树的序列化和反序列化
  • 应用程序部署(IIS的相关使用,sql server的相关使用)
  • 小程序源码-模版 100多套小程序(附源码)
  • UE5运行时创建slate窗口
  • 浅谈C#之单线程流式适配器
  • 【更新中】《硬件架构的艺术》笔记(三):处理多个时钟
  • 【matlab】数据类型01-数值型变量(整数、浮点数、复数、二进制和十六进制)
  • 引入第三方jar包部署服务器后找不到jar处理方法
  • neo4j desktop基本入门
  • 前端系统设计面试题(二)Javascript\Vue
  • 军工行业运维:监控易引领自主可控新潮流
  • unity3d————接口基础知识点
  • 蓝队基础5 -- 安全策略与防护技术
  • 【Bluedroid】A2dp初始化流程源码分析
  • Redis简介、数据结构、高性能读写、持久化机制、分布式架构
  • 鸿蒙自定义UI组件导出使用
  • python os.path.join 详解
  • JavaScript高效处理CSV文件的操作指南
  • Go开发指南- Goroutine
  • Dubbo 3.x源码(24)—Dubbo服务引用源码(7)接口级服务发现订阅refreshInterfaceInvoker
  • 高级java每日一道面试题-2024年11月04日-Redis篇-Redis如何做内存优化?
  • 数据结构 -二叉搜索树