MyBatis primary key issue in Oracle

SQL Snippet in XML Configuration:

<insert id="insertSelective" parameterType="com.jxxx.p2pp.model.UUserInfo">
    <selectKey resultType="java.math.BigDecimal" order="BEFORE" keyProperty="id">
	   SELECT U_USER_INFO_SEQ.Nextval as ID from DUAL
   </selectKey>
	
    insert into U_USER_INFO
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        ID,
      </if>
      <if test="userName != null" >
        USER_NAME,
      </if>
      <if test="realName != null" >
        REAL_NAME,
      </if>
    .....
</insert>

 

The point is used here to define selectKey return PrimaryKey new generation, this applies only to Oracle.

Need local attention is the use of type Integer in Java code. But in MyBatis mapping file. Use java.math.BigDecimal type, otherwise an error or does not match the type of conversion will be reported.

 

Other example MySQL or SQLServer situation applies to the following cases:

 

    <insert id="insert" parameterType="Spares"   
            useGeneratedKeys="true" keyProperty="id">  
            insert into spares(spares_id,spares_name,  
                spares_type_id,spares_spec)  
            values(#{id},#{name},#{typeId},#{spec})  
        </insert>  

Use useGeneratedKeys / KeyProperty achieved when the insert data, to generate the primary key of the newly completed return.

 

 

 

 

Among exception information solutions:

Exception information:

   org.springframework.jdbc.UncategorizedSQLException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 无效的列类型: getBigDecimal not implemented for class oracle.jdbc.driver.T4CRowidAccessor
; uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: getBigDecimal not implemented for class oracle.jdbc.driver.T4CRowidAccessor; nested exception is java.sql.SQLException:
无效的列类型: getBigDecimal not implemented for class oracle.jdbc.driver.T4CRowidAccessor

 

problem solved:

    The problem is that the primary key data type in Java code is set to return, the return data type which is java.lang.Integer, rather than BigDecimal and Long. But the type of MyBatis mapping file is in java.math.BigDecimal

Guess you like

Origin www.cnblogs.com/lingcheng7777/p/11910601.html