Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TypeHandler for INSERT statements in MyBatis

Tags:

java

mybatis

There is configuration:

<resultMap id="mapId" type="package.MyType">
    <result property="prop1" column="column1" />
    <result property="prop2" column="column2" />
    <result property="prop3" column="column3" typeHandler="package.MyTypeHandler" />
</resultMap>

<select id="selectStat" resultMap="mapId">
    SELECT `column1`, `column2`, `column3` 
    FROM `table`; 
</select>

For select statement all is fine, handler is invoked.

How can i write INSERT statement to invoke the same handler for column3 when inserting data?

like image 885
r.r Avatar asked Oct 01 '14 12:10

r.r


People also ask

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.

What is parameterType in MyBatis?

parameterType. The fully qualified class name or alias for the parameter that will be passed into this statement. This attribute is optional because MyBatis can calculate the TypeHandler to use out of the actual parameter passed to the statement. Default is unset .

Does MyBatis use prepared statement?

MyBatis does four main things: It executes SQL safely and abstracts away all the intricacies of JDBC. It maps parameter objects to JDBC prepared statement parameters. It maps rows in JDBC result sets to objects.


1 Answers

You can use INSERT statement as follows.

<insert parameterType='myType' >
  INSERT into table(column1, column2, column3) values(#{prop1},#{prop2},#{prop3,typeHandler=package.Typehandler})
</insert>

Edit : use typeHandler= and not typehandler=

like image 130
Karthik Prasad Avatar answered Oct 03 '22 17:10

Karthik Prasad