What is the custom method for mybatis typehandler?
MyBatis TypeHandler is a tool used for converting between database types and Java types, allowing for customization of conversion logic through custom methods.
To create a custom TypeHandler, you need to implement the org.apache.ibatis.type.TypeHandler interface and override its methods. Some commonly used methods in the TypeHandler interface are as follows:
- setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType):Sets a Java type parameter into a PreparedStatement object for use when executing SQL statements.
- getResult(ResultSet rs, String columnName): Retrieve the value of the specified column name from the ResultSet object, convert it to the Java type, and return the result.
- getResult(ResultSet rs, int columnIndex): Retrieve and convert the value from the specified column index in the ResultSet object to a Java type, and return the result.
- Retrieve the value from the specified column index in the CallableStatement object, convert it to a Java type, and return the result.
- javaType() returns the Java type handled by the TypeHandler.
- jdbcType(): returns the database type handled by the TypeHandler.
- In the setParameter method, this method will be called only when the parameter is not null, and it is used to set a non-null value to the PreparedStatement object.
By rewriting these methods, custom type conversion logic can be implemented to handle specific requirements accordingly.