Search code examples
javaibatismybatis

How do I insert a java List<String> using Mybatis?


I would like to insert a list of strings into a single column in my database using Mybatis. I've tried using a Custom TypeHandler but I can't even get Mybatis to invoke it.

For a more detailed report on what I've already done click here


Solution

  • While specifying parameters for INSERT statement do like this:

    INSERT INTO tableName(a) VALUES(#{aVal, typeHandler=com.test.YourTypeHandler})
    

    where aVal is the parameter that you have passed to statement. Also intead of full name of typehandler you can use it alias. But don't forget to register it(typeHandler) in configuration file of MyBatis

    edited

    A good practise is specifying a type of value to be inserted like this: #{aVal, jdbcType=VARCHAR, typeHandler=com.test.YourTypeHandler}. It will save you from issues with null values of aVal