插入参数中的我的Batis Type转换错误

时间:2014-01-02 13:32:11

标签: java spring mybatis ibatis

我是mybatis的新手。我的班级里有一个java BigInteger属性。相关的mysql字段类型是BigInt。当我要使用xml配置文件向数据库插入记录时,它会给出'There was no TypeHandler found for parameter id'之类的异常。

我的xml插入查询是,

INSERT INTO xX_ (
    id, date, name, connection)
    VALUES (
    #{id}, #{date}, #{name}, #{connection}
)

请帮助我解决此问题,而无需更改DTO课程的类型。

3 个答案:

答案 0 :(得分:1)

请看这里:http://mybatis.github.io/mybatis-3/configuration.html#typeHandlers

要使用BigInteger和BigInt,您必须编写自定义typeHandler,如下所示:

// ExampleTypeHandler.java
@MappedTypes(BigInteger.class)
public class BigIntegerTypeHandler extends BaseTypeHandler<BigInteger> {

  //implement all methods
}

像这样配置你的config.xml:

<!-- mybatis-config.xml -->
<typeHandlers>
  <typeHandler handler="your.packadge.BigIntegerTypeHandler"/>
</typeHandlers>

一定有帮助。

答案 1 :(得分:0)

试试这种方式

@Options(useGeneratedKeys = true, keyProperty = "id")
void createDetail(myDetail md);


<insert id="createDetail" parameterType="myDetail">
    INSERT INTO xX_
       ( id,
         `date`,
         `name`,
         `connection`)
    VALUES ( #{id,     javaType=BigInteger,    jdbcType=BIGINT},
             #{date},
             #{name},
             #{connection} )

    <selectKey keyProperty="id" resultType="java.math.BigInteger">
        SELECT LAST_INSERT_ID()
    </selectKey>
</insert>

尝试使用不同的javaType,resultType和jdbcType。例如,javaType = java.math.BigInteger javaType = Long,javaType = java.lang.Long,jdbcType = NUM​​ERIC,jdbcType = DECIMAL,resultType =“java.lang.Long”。

parameterType =“myDetail”必须等于类名,但不能等于类实例的名称。如果您的类名是com.yourcompany.MyDetail,实例名是myDetail,请尝试使用parameterType =“MyDetail”或parameterType =“com.yourcompany.MyDetail”

另见"Table 5.2 MySQL Types to Java Types for ResultSet.getObject()"

MySQL Type Name: BIGINT[(M)] [UNSIGNED]

Returned as Java Class: java.lang.Long, if UNSIGNED java.math.BigInteger

答案 2 :(得分:0)

我以前在工作的地方使用MyBatis和Spring。

我们以前做过如下声明 注意我们设置为jdbcType = VARCHAR的userId,然后DB驱动程序正确地转换为BigInt或我们正在使用的任何东西。

我认为我们在系统中的对象中使用了Long。

<delete id="deleteJob" parameterType="com.beans.JobDetailVO">
    UPDATE JOBS.JOB
    SET
        DO_NOT_USE=#{doNotUse}
    WHERE
        JOB_ID=#{postedJobID} and USER_ID=#{userID,jdbcType=VARCHAR}
</delete>

至于插入,我们做了以下

 <insert id="insertJobDetail" parameterType="com.beans.JobDetailVO">

        INSERT INTO JOBS.JOB
        (
        JOB_ID,
        RECRUITER_TYPE_ID,
        USER_DETAIL_ID,
        HIDE_RECRUITER_INFO,
        TITLE, ....
        )
        VALUES (
        #{jobID,jdbcType=VARCHAR},
        #{recruiterTypeID ,jdbcType=VARCHAR},
        #{userDetailID ,jdbcType=VARCHAR},
        #{hideRecruiterInfo ,jdbcType=VARCHAR},
        #{jobTitle ,jdbcType=VARCHAR},
            .....
        )
        <selectKey resultType="java.lang.Long" statementType="STATEMENT" keyProperty="JobID" order="BEFORE">SELECT
            JOBS.S_JOB.NEXTVAL AS JOBID FROM DUAL
        </selectKey>

    </insert>
相关问题