我在项目中使用MyBatis,当我遇到如下问题时,我不知道如何向查询传递两个参数,它总是告诉我找不到参数,我不知道为什么, 感谢您的帮助。 配置映射器:
@Select("select * from tq_configure_pacific where visitTime between #{dateFrom,jdbcType=DATE} and #{dateTo,jdbcType=DATE}")
@Options(flushCache = true)
List<configure> selectConfigureByTime(Date dateFrom,Date dateTo);
configurMapper.xml
<select id="selectConfigure2" >
select * from tq_configure_pacific where visitTime between #{dateFrom,jdbcType=DATE} and #{dateTo,jdbcType=DATE}
</select>
主要功能:
public static void main(String[] args) throws SQLException, IOException{
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
try{
configureMapper mapper = session.getMapper(configureMapper.class);
// configure conf = mapper.selectConfigureById(500);
Calendar cal=Calendar.getInstance();
long nextday=cal.getTime().getTime()+24*60*60*1000;
Date visitDate=new java.sql.Date(cal.getTime().getTime());
Date validDate=new java.sql.Date(nextday);
System.out.println(visitDate);
System.out.println(validDate);
List<configure> conf2 = mapper.selectConfigureByTime(visitDate,validDate);
for(configure confg:conf2)
{
System.out.println(confg.getId()+"=="+confg.getValidTime());
}
// System.out.println("Source name:"+conf2.getSource());
}finally{
session.close();
}
}
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'dateFrom' not found. Available parameters are [0, 1, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'dateFrom' not found. Available parameters are [0, 1, param1, param2]
最佳答案
MyBatis 无法通过 java 方法参数名称来检测 params。因此,如果您的方法中有超过 1 个参数,您应该使用 org.apache.ibatis.annotations.Param 注释来注释它们:
List<configure> selectConfigureByTime(@Param("dateFrom") Date dateFrom, @Param("dateTo") Date dateTo);
关于mysql - mybatis选择两个时间之间的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26387699/