使用mapper查询数据库参数传递失败

错误日志:

.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘task_type’ not found. Available parameters are:
如图:在这里插入图片描述
错误代码具体描述:

需求: 根据task_type,priority,futureTime查询数据库taskinfo的所有数据:

测试类代码如下:

package com.chongba.service;

import com.chongba.schedule.ScheduleApplication;
import com.chongba.schedule.conf.SystemParams;
import com.chongba.schedule.mapper.TaskInfoMapper;
import com.chongba.schedule.pojo.TaskInfoEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Calendar;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ScheduleApplication.class)
public class MapperTest {

    @Autowired
    TaskInfoMapper taskInfoMapper;//mapper

    @Autowired
    private SystemParams systemParams;//时间参数配置类 //系统参数配置类

    private long nextScheduleTime;//存储时间值
    @Test
    public void selectMaps(){
        //获取未来5分钟的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE,systemParams.getPreLoad());
        nextScheduleTime = calendar.getTimeInMillis();
        //数据查询
        List<TaskInfoEntity> allTaskInfo = taskInfoMapper.queryFutureTime(1001,4,calendar.getTime());
        //遍历数据
         for (TaskInfoEntity taskInfoEntity : allTaskInfo) {
            System.out.println(taskInfoEntity);
        }
    }

TaskInfoMapper连接数据库类:

package com.chongba.schedule.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chongba.schedule.pojo.TaskInfoEntity;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;
//import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public interface TaskInfoMapper extends BaseMapper<TaskInfoEntity> {
    //查询所有
    @Select("select * from taskinfo")
    List<TaskInfoEntity> selectAll();
    //根据优先级和类型查询所有任务
    @Select("select * from taskinfo where task_type = #{task_type} and priority = #{priority}")
    List<TaskInfoEntity> queryAll(@Param("task_type")int type, @Param("priority") int priority);
    //根据优先级和类型及时间查询所有任务
    @Select("select * from taskinfo where task_type = #{task_type} and priority = #{priority} and execute_time <= #{future,javaType=java.util.Date}")
    List<TaskInfoEntity> queryFutureTime(@Param("task_type")int task_type, @Param("priority") int priority,@Param("future") Date future);
}

数据库结构:
在这里插入图片描述

执行测试类报错: 但是查询所有发现可以查询:测试方法如下:

  @Test
    public void test(){
        List<TaskInfoEntity> all = taskInfoMapper.selectAll();
        for (TaskInfoEntity taskInfoEntity : all) {
            System.out.println(taskInfoEntity);
        }
    }

控制台运行结果 发现可以正常运行:
在这里插入图片描述
错误原因:mapper使用@Param注解时不起作用,导包出现的问题,更改导包即可如下:
在这里插入图片描述
更改导包:
在这里插入图片描述
再次运行测试成功:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值