如何在Java中迭代表格行?

huangapple go评论86阅读模式
英文:

How to iterate through table rows in Java?

问题

我想要通过JPA迭代访问SQL表格,以下是使用示例:
假设我有一个包含100列的"Person"表格:

在我的Java程序中,我想要执行以下操作(仅作为示例):

  • 对于每一行数据
  • 获取"firstname"字段
  • 获取"number"字段
  • 对"firstname"字段执行操作
  • 对"number"字段执行操作

问题是,我不想创建一个"Person"的实体类,因为它包含了100列,显得很麻烦,而且我对其中的大部分字段都不感兴趣。

另外,我也不想以Object[]类型来检索行数据,因为假设我只想提取其中的40列,那么操作这样的数据类型会很混乱。

我正在使用Spring Data JPA而非JDBC。

英文:

I want to iterate through an sql table using JPA, example of use:
I have the table Person which contains 100 columns

id | gender | firstname | lastname |  address1 | address2 | number | ....+100
------------------------------------------------------------------------------
-  |  ...   |   .....   |   ...    |   ....    |   ....   |   ..   |  ....
------------------------------------------------------------------------------
-  |  ...   |   .....   |   ...    |   ....    |   ....   |   ..   |  ....

and in my Java program, I want to do the following (as an example):

  • for each row
  • get the firstname
  • get the number
  • do operation with firstname
  • do operation with number

the problem is that I don't want to create an Entity class of Person because it contains 100 columns and it's quite cumbersome and I'm not interested with all of them.

Also, I don't want to retrieve the row in an Object[] type, because suppose that I want to extract 40 columns out of 100, it will be confusing to manipulate such a type.

I'm using spring data JPA and not JDBC.

答案1

得分: 1

你可以创建一个仅包含你感兴趣列的实体类。

然后,使用 Spring 数据,你将获得一个 Person 类的集合,你可以按照你喜欢的方式进行迭代。

英文:

You can create an entity class with only the columns you're interrested in.

And then, with Spring data, you will get a collection of Person class which you can iterate through the way you like.

huangapple
  • 本文由 发表于 2020年8月23日 17:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63545170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定