Tuesday, July 1, 2014

Get data table metadata using MyBatis

Don't try to use myBatis type handlers to get the metadata of some database tables.
If the datatable doesn't contain any data the type handler won't work and you will not get any result.
There is a link about how to do it with TypeHandlers but don't forget that your datatable shoudn't be empty:
http://www.coderanch.com/t/415314/ORM/databases/retrieve-metadata-ibatis
http://www.thespringriver.com/simple-example-of-mybatis-java-maven-implementation-8-customized-type-handler/ (This is about how to use TypeHandler for the latest MyBatis version [3.2.7])

The better solution is using the data from INFORMATION_SCHEMA, for example you can use some query like:

<select id="selectMeta" parameterType="String" resultType="DataColumnForm">
 SELECT TABLE_CATALOG, TABLE_NAME, COLUMN_NAME, TYPE_NAME, DATA_TYPE, COLUMN_DEFAULT
  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=#{tbl_name}  
</select>

No comments:

Post a Comment