Question

I need to get the list of columns in a table using mybatis/ibatis in java 1.5.

Was it helpful?

Solution

That's not a typical requirement (99.99% of applications using iBatis or whatever ORM knows the DB schema). iBatis is a SQL mapper, you must write the SQL query yourself. And there is no standard SQL query (AFAIK) that gives you the number of columns in a table.

I can only suggest two approaches:

  1. Make a SQL query selecting from the catalog tables. That's the normal way of knowing about your DB metadata. But that depends on your particular database engine. And it's not related to iBatis.

  2. QUick and dirty: make an ad-hoc query SELECT * FROM MYTABLE LIMIT 1 (replace LIMIT for your DB analog), map that in iBatis through a HashMap, and in your DAO just count the number of keys.

OTHER TIPS

For Mybatis:You need to use resultType instead of resultmap. resultType must be of returning collection data type, by knowing the size of collection you can get no. of columns and more over if you are going with HashMap you can get column names too in keys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top