27082

mybatis custom type handler without annoations

I'm new to mybatis. I am trying to map a JDBC integer to a custom class. All the examples that I have seen on this have used annotations, is it possible to not use annotations and do this? Any example would be greatly appreciated.

Sreekanth

Answer1:

It is definitely possible and is described in general in Configuration and in Mapper sections of the documentation.

Define the handler first:

@MappedJdbcTypes(JdbcType.INTEGER) public class MyClassHandler extends BaseTypeHandler<MyClass> { @Override public void setNonNullParameter(PreparedStatement ps, int i, MyClass parameter, JdbcType jdbcType) throws SQLException { ps.setInt(i, parameter.asInt()); } @Override public MyClass getNullableResult(ResultSet rs, String columnName) throws SQLException { int val = rs.getInt(columnName); if (rs.wasNull()) return null; else return MyClass.valueOf(val); } @Override public MyClass getNullableResult(ResultSet rs, int columnIndex) throws SQLException { int val = rs.getInt(columnIndex); if (rs.wasNull()) return null; else return MyClass.valueOf(val); } @Override public MyClass getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { int val = cs.getInt(columnIndex); if (cs.wasNull()) return null; else return MyClass.valueOf(val); } }

Then configure it in mybatis-config.xml:

<typeHandlers> <typeHandler handler="my.company.app.MyClassHandler"/> </typeHandlers>

Now you can use it in xml mappers. If you have a class

class SomeTypeEntity { private MyClass myClassField; };

For querying the field configure handler in the resultMap like this:

<resultMap id="someMap" type="SomeTypeEntity"> <result property="myClassField" column="my_class_column" typeHandler="my.company.app.MyClassHandler"/> </resultMap>

For insert/update use it like this:

<update id="updateSomeTypeWithMyClassField"> update some_type set my_class_column = @{someTypeEntity.myClassField, typeHandler=my.company.app.MyClassHandler}, </update>

for mapper method:

void updateSomeTypeWithMyClassField(@Param("someTypeEntity") SomeTypeEntity entity);

Recommend

  • Using type handler in where clause in MyBatis
  • change braces postion with sed
  • freegeoip doesn't work anymore
  • enumerating all possible strings of length K from an alphabet in Python [duplicate]
  • Fast vectorized conversion from RGB to BGRA
  • RecyclerView scrolling top when loading new data
  • set and get value using Session storage for html drop down with jquery
  • Referencing dictionaries in Python
  • Dynamically derive a class in python [duplicate]
  • Create new column from specific rows in pandas dataframe
  • Left factoring grammar
  • How to find and replace, skipping lines containing a particular character?
  • Atmel SAM3X dual bank switching not working
  • Saving java object to PostgreSQL problem
  • TSQL: Prevent trigger suppressing error but rolling back transaction
  • Why is this regex failing when adding anchors?
  • Issues with unchecked generics
  • PreparedStatement - how specify to use default value of column
  • jquery ui datepicker error on beforeShowDay
  • Insert Path of a file with \\\\ in mysql using java
  • Why does this empty dict break shared references?
  • Ruby and class variables in inherit class
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Akka Routing: Reply's send to router ends up as dead letters
  • Suggestions to manage Login/Logout transitions
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?