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?
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.
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 .
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.
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=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With