简体   繁体   中英

How can I get byte[] from bytea column with MyBatis?

I tried the following way.

Here is the query mapping:

<select id="getTypicalTaskMeasurementParameterValue" 
            parameterType="Integer" 
            resultType="byte[]">
    SELECT value 
    FROM typical_task_measurements_parameter_values 
    WHERE id_typical_task_measurement = #{typicalTaskMeasurementId}
</select>

Here is the method:

public byte[] getTypicalTaskMeasurementParameterValue(
    Integer typicalTaskMeasurementId);

And here is the error I got, trying to run the unit test against it:

nested exception is org.apache.ibatis.reflection.ReflectionException: 
Error instantiating class [Ljava.lang.Byte; with invalid types () or values (). 
Cause: java.lang.NoSuchMethodException: [Ljava.lang.Byte;.<init>()
at ...

Moreover, setter method for this bytea staff is ok.

The error message says the problem pretty well. There is no default constructor on java.lang.Byte .

You need a result map that will choose which constructor to use, or implement your own TypeHandler.

first get Object instead of byte[]

public Object getTypicalTaskMeasurementParameterValue(
    Integer typicalTaskMeasurementId
);

Then change Object to byte[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM