mysql - mybatis选择两个时间之间的记录

标签 mysql mybatis

我在项目中使用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/

相关文章:

mysql - 如何修复字段中的错误 #1054 未知列

java - 嵌套类: `OuterClass.this.someAttribute` ?

java - 带有 MyBatis TypeHandler 的空对象模式

java - MyBatis-Spring PersistenceExceptionTranslator - SQL 语句?

php - 又一个 'Call to a member function on a non-object' 问题

java - Spring Rest Hibernate 一对多/多对一

MySQL INSERT 重复键 UPDATE 与 SELECT

java - 如何从变量设置数据库登录信息?

java - 从数据库中选择后如何使用mybatis获取结果类型作为列表

javascript - 从mysql获取数据并以另一种形式使用它