有没有一种通用的方法来获取MapRow的ResultsSet中的列 [英] Is there a generic way of getting columns in ResultsSet of MapRow

查看:419
本文介绍了有没有一种通用的方法来获取MapRow的ResultsSet中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimpleJdbcTemplate,例如,我有这样的内容:

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"));

所以我必须查看我的表,看看我应该为每一列使用哪种类型,-在此示例中为String的getString-...所以我将拥有getString,getLong,getInt,...

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,...

我想知道是否存在一种更通用的方式来从结果集中获取这些值,而无需指定正确的类型,并希望Spring JDBC处理这些通用类型上的装箱/拆箱操作

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

推荐答案

如果要将JDBC结果映射到对象模型,则必须忍受这一点.使用JDBC就是这样.

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.

如果您想要更高级的内容,包括列到属性的映射,那么您需要一个更好的工具.您可以全程使用Hibernate,但这会带来很多负担,并且解决的每个问题都会带来10个新问题.

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.

看看 MyBatis (以前称为iBatis).这是一个相当基本的框架,用于将JDBC结果集映射到javabeans,并支持连接/语句管理.Spring

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.

这篇关于有没有一种通用的方法来获取MapRow的ResultsSet中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆