mybatis选择两次之间的记录

时间:2014-10-15 16:39:40

标签: mysql mybatis

我在我的项目中使用MyBatis,当我遇到如下问题时,我不知道如何将两个参数传递给查询,它总是告诉我找不到参数,我不知道为什么, 感谢您的帮助。 配置Mapper:

  @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]
我做错了吗?

1 个答案:

答案 0 :(得分:2)

MyBatis无法通过java方法参数名称检测params。因此,如果您的方法中有多个参数,则应使用org.apache.ibatis.annotations.Param注释对其进行注释:

List<configure> selectConfigureByTime(@Param("dateFrom") Date dateFrom, @Param("dateTo") Date dateTo); 
相关问题