SQL 使用 MyBatis 异常

SQL exception using MyBatis

我遇到了问题。当 运行ning 这个 MyBatis select:

<select id="findIdByCode" resultType="long">
        select USER_ID from PWD_REST_CODE
        where RESTORATION_CODE = #{code}
        and current_date between date_from and DATE_FROM + interval #{interval} second
</select>

我遇到 EJB 异常:

EJB exception occurred during invocation from home or business: 
com.project.auth.ejb.data.UserManagerBean_rdl8zk_Intf generated exception: 
org.apache.ibatis.exceptions.PersistenceException: ### Error querying 
database. Cause: java.sql.SQLSyntaxErrorException: ORA-00933: неверное 
завершение SQL-предложения ### The error may exist in 
com/project/auth/dao/UserDAO.xml ### The error may involve 
defaultParameterMap ### The error occurred while setting parameters ### SQL: 
select USER_ID from PWD_REST_CODE where RESTORATION_CODE = ? and current_date 
between date_from and DATE_FROM + interval ? second ### Cause: 
java.sql.SQLSyntaxErrorException: ORA-00933: неверное завершение SQL-предложения 

(неверное завершение SQL-предложения代表SQL command not properly ended)

但是当我在数据库管理器中运行完全相同的查询时:

    select USER_ID from PWD_REST_CODE
    where RESTORATION_CODE = '217799dfHj'
    and current_date between date_from and DATE_FROM + interval '86400' second

我得到了应得的 id (user_id, 5)。为什么会这样?

正如 Marmite Bomber 在评论中正确写道,您不能在 INTERVAL 文字中使用数字。使用numToDSInterval函数将数字转换为区间:

<select id="findIdByCode" resultType="long">
     select USER_ID from PWD_REST_CODE
     where RESTORATION_CODE = #{code}
         and current_date between date_from
         and DATE_FROM + numToDSInterval( #{interval}, 'second' )
</select>