问题
I am using SimpleJdbcTemplate and for example I have something like this:
@Override
public Variant mapRow(ResultSet rs, int rowNum) throws SQLException
then I am getting the values from this result set with lines of code like this:
variant.setName(rs.getString("variant_name"));
so I have to look at my table, see what type should I use for each column, - getString for String in this example - ...so I will have getString, getLong, getInt,...
I was wondering if there a more generic way of getting these values from result set without the need to specify the correct type and hope that Spring JDBC takes care of some boxing/unboxing on these generic types
回答1:
If you want to map JDBC results to your object model, then you're going to have to live with doing that. That's the deal when you use JDBC.
If you want something more high level, including column-to-property mapping, then you need a better tool. You could go the whole hog and use Hibernate, but that carries a whole load of baggage, and presents 10 new problems for every one it solves.
Have a look at MyBatis (formerly known as iBatis). This is a pretty basic framework for mapping JDBC result sets to javabeans, with connection/statement management backed in. Spring provides support for iBatis 2, but iBatis 2 itself is no longer supported. The new MyBatis 3.x isn't supported by Spring out-of-the-box, but the MyBatis project does provide it's own Spring integration.
来源:https://stackoverflow.com/questions/7016753/is-there-a-generic-way-of-getting-columns-in-resultsset-of-maprow