Search code examples
javamysqlarraysinnodbmybatis

How to insert simple array into table field using mybatis annotation


My class field looks like

private int[] points = new int[]{1,1,1,1};

My innoDb table looks like

CREATE TABLE `test` (
    `points` VARCHAR(70) NULL DEFAULT '0' 
)

I try insert row into table thith this mapper (i'm using annotation)

@Insert({
        "REPLACE INTO test (points)",
        "values (#points},javaType=java.util.Array,typeHandler=org.apache.ibatis.type.ArrayTypeHandler)"
})

and getting the java.lang.IllegalStateException: No typehandler found for property points

How i can correctly insert this array in one field? I could convert an array into a string, but I want to use the mybatis opportunity.


Solution

  • I see a typo in the snippet for Mybatis parameter, on curly braces {}:

    VALUES ( {#points,javaType=java.util.Array,typeHandler=org.apache.ibatis.type.ArrayTypeHandler}
    )
    

    Furthermore, a custom ArrayTypeHandler implementation could be required, either for formatting as here it is stored as a String(Varchar), or if it is stored as SQL Array, then dependent on environment: DB Type/driver, pooled connection, application server ...