【问题解决】:nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘gender‘ not found.

一、问题描述

在执行查找操作时
接口代码如下:

//按照条件查找员工
    @Select("select * from emp where name like '%${name}%' and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc")
    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);

测试代码如下:

    @Test
    public void testList(){
        List<Emp> empList =  empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));
        System.out.println(empList);
    }

报错信息如下:
在这里插入图片描述

二、问题分析

在Springboot的1.x版本中或者单独使用mybatis,在编译过程中并不会保留形参名称,编译完成之后就全部变成了var(如果使用Springboot2.x就不会)
在这里插入图片描述

三、解决方法

这里使用@Param注解,这种方式Mybatis就会自动将参数封装成Map类型,@Param注解的值会作为Map中的key,例如:

Mapper接口中:

    public List<Emp> list(@Param("name")String name, @Param("gender") Short gender,@Param("begin") LocalDate begin,@Param("end") LocalDate end);

修改后查找结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值