【问题标题】:SQL exception using MyBatis使用 MyBatis 的 SQL 异常
【发布时间】:2018-10-25 14:49:13
【问题描述】:

我遇到了问题。运行这个 MyBatis 时选择:

<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)。为什么会这样?

【问题讨论】:

  • interval '86400' second 与您在 MyBasic 中使用的 interval 86400 second 有点不同。我想因为类型是 NUMBER MyBatis 不会在它周围添加引号。

标签: sql oracle mybatis dao


【解决方案1】:

正如 Marmite Bomber 在 cmets 中正确写的那样,您不能在 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>

【讨论】:

    猜你喜欢
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多