错误日志:
.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注解时不起作用,导包出现的问题,更改导包即可如下:
更改导包:
再次运行测试成功: