wild84 2017-08-14 04:15 采纳率: 81.8%
浏览 1574
已采纳

oracle设置自动增长id,mybatis如何插入数据?

我的mybatis的xml mapper文件语句如下:

 <insert id="insertFlights" parameterType="com.entity.Flights" useGeneratedKeys="true" keyProperty="id">
        insert into FLIGHTS(ID,FLIGHT_CODE,FLIGHT_DATE,AIRLINE,TYPE,TAKE_AIRPORT_ID,LANDING_AIRPORT_ID,TAKE_TIME,LANDING_TIME,FLIGHT_TIME,STOP_AIRPORT,REFERENCE_PRICE) 
        values(#{id},#{flightCode},#{flightDate},#{airline},#{type},#{takeAirportId},#{landingAirportId},#{takeTime},#{landingTime},#{flightTime},#{stopAirport},#{referencePrice})
    </insert>

报的异常如下:

 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='id', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111

如果我把xml mapper里的语句去掉id字段;则报异常如下:

 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column #1 from result set.  Cause: java.sql.SQLException: 请求的转换无效

请问应该怎么处理?

  • 写回答

3条回答 默认 最新

  • NightGone 2017-08-14 09:50
    关注

    你的语法有问题,既然你设置了数据库自增长,成功返回数据ID,那么为什么还要添加ID属性?参考:
    insert into FLIGHTS(
    FLIGHT_CODE,FLIGHT_DATE,AIRLINE,TYPE,TAKE_AIRPORT_ID,LANDING_AIRPORT_ID,TAKE_TIME,LANDING_TIME,FLIGHT_TIME,STOP_AIRPORT,REFERENCE_PRICE)
    values(
    #{flightCode},#{flightDate},#{airline},#{type},#{takeAirportId},#{landingAirportId},#{takeTime},#{landingTime},#{flightTime},#{stopAirport},#{referencePrice})

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?