Ibatis - 查询列表

发布于 2024-10-20 06:48:29 字数 135 浏览 1 评论 0原文

你好 这可能是基本问题,但我是 Ibatis 新手。 queryForList 将返回哪些对象的列表?通常,对于queryForObject,我们定义一个resultMap,但对于queryForList,没有定义这样的映射。

提前致谢。

Hi
This might be basic question but I am new to Ibatis.
queryForList will return list of which objects? Generally for queryForObject we define a resultMap but no such map is defined for queryForList.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

煞人兵器 2024-10-27 06:48:29

您只需为查询定义一个 resultMap,无论它应该返回多少行。如果您调用 queryforObject(),那么 iBatis 将假定查询应该只返回一行。如果我没记错的话,如果它返回更多行,它会抛出异常。如果您调用 queryForList(),那么 iBatis 将假定查询可以返回多于一行。在这两种情况下,iBatis 返回的每一行都会基于 resultMap 进行转换,因此您可以将 resultMap 视为如何将单个行转换为单个对象的配方。

You just define a resultMap for query regardles of how many rows it is supposed to return. If you call queryforObject() then iBatis will assume that query schould return just one row. If I remember correctly it will throw exception if it returns more rows. If you call queryForList() then iBatis will assume query can return more than one row. In both scenarios for every row returned iBatis will do transformation based on resultMap, so you can think about resultMap as a recipe how to convert a single row into a single object.

乞讨 2024-10-27 06:48:29

假设您的映射 XML 中有以下配置:

<select id="getFoo" resultClass="java.util.HashMap">
  select acctNbr, name, address
  from addressfile
  order by name
</select>

然后您可以使用 queryForList 以列表形式检索结果集并迭代其条目,如下所示:

List<Map<String,Object>> results = (List<Map<String,Object>>) sqlMap.queryForList("getFoo");
for (Map<String,Object> entry : results) {
  String acctNbr = entry.get("acctNbr");
  String name = entry.get("name");
  String address = entry.get("address");
  System.out.println(acctNbr + " : " + name + " : " + address + "\n");
}

Let's say you have the following configuration in your mapping XML:

<select id="getFoo" resultClass="java.util.HashMap">
  select acctNbr, name, address
  from addressfile
  order by name
</select>

Then you can use queryForList to retrieve the result set in List form and iterate through its entries, like this:

List<Map<String,Object>> results = (List<Map<String,Object>>) sqlMap.queryForList("getFoo");
for (Map<String,Object> entry : results) {
  String acctNbr = entry.get("acctNbr");
  String name = entry.get("name");
  String address = entry.get("address");
  System.out.println(acctNbr + " : " + name + " : " + address + "\n");
}
笑着哭最痛 2024-10-27 06:48:29

queryForList 也可以使用 resultMap 作为 List 的项目。
我们假设您有以下 sql-map 文件。

<resultMap class="me.skpark.bean.CustomBean" id="custombean">
    <result property="v1" column="VALUE1" />
    <result property="v2" column="VALUE2" />
    <result property="v3" column="VALUE3" />
</resultMap>

<select id="select2" parameterClass="java.util.HashMap" resultMap="custombean" >
    select VALUE1, VALUE2, VALUE3from system_control_m
</select>

您可以使用 List 作为接收数据。

HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "park");
List<CustomBean> list = sqlMap.queryForList("select2", params);

queryForList is also can use resultMap as List's item.
we assume that you have the following sql-map file.

<resultMap class="me.skpark.bean.CustomBean" id="custombean">
    <result property="v1" column="VALUE1" />
    <result property="v2" column="VALUE2" />
    <result property="v3" column="VALUE3" />
</resultMap>

<select id="select2" parameterClass="java.util.HashMap" resultMap="custombean" >
    select VALUE1, VALUE2, VALUE3from system_control_m
</select>

you can use List as receive data.

HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "park");
List<CustomBean> list = sqlMap.queryForList("select2", params);
A君 2024-10-27 06:48:29

是的,没错,如果使用 queryForObject() 时找到多个记录,iBatis 将抛出异常。

如果您使用queryForList,您可以定义一个resultMap,我宁愿使用返回的类类型定义我的resultMap,并在那里进行属性到列的映射。我认为这看起来更干净(看看你的 DAO 类,它很容易阅读和抽象)并且抽象得更好。但这是我的意见。

<resultMap id="accountsResult" class="xx.co.xx.AccountsDTO">
    <result property="id" column="id" jdbcType="NUMERIC" javaType="java.lang.Long"/>
    <result property="name" column="name" jdbcType="NUMERIC" javaType="java.lang.Long"/>
</resultMap>

<select id="retrieveAccounts" resultMap="accountsResult">
    SELECT
    acounts.id,
    accounts.name
    FROM accounts
</select>

queryForList(“检索帐户”);

Yep that's right iBatis will throw an exception if more than one record is found when queryForObject() used.

If your using queryForList you can define a resultMap and I would rather define my resultMap with the returned class type and do the property to column mapping there. I think this looks more clean (looking at your DAO class it is easy to read and abstracted) and better abstracted. However this is my opinion.

<resultMap id="accountsResult" class="xx.co.xx.AccountsDTO">
    <result property="id" column="id" jdbcType="NUMERIC" javaType="java.lang.Long"/>
    <result property="name" column="name" jdbcType="NUMERIC" javaType="java.lang.Long"/>
</resultMap>

<select id="retrieveAccounts" resultMap="accountsResult">
    SELECT
    acounts.id,
    accounts.name
    FROM accounts
</select>

queryForList("retrieveAccounts");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文