C# 你如何在 iBatis 中映射 List<string> ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1127992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:51:29  来源:igfitidea点击:

How do you map a List<string> in iBatis?

c#mappingibatis.net

提问by Brandon

I have a class like this

我有一堂这样的课

public SomeClass
{
   private List<string> _strings = new List<string>();

   public IEnumerable<string> Strings
   {
      {  get return _strings; }
   }
}

How would I do the mapping for _strings?

我将如何为 _strings 进行映射?

I tried this, but it complains about the List typehandler not being found, which it doesn't complain about if I mapped it as an object.

我试过这个,但它抱怨没有找到 List 类型处理程序,如果我将它映射为一个对象,它不会抱怨。

<result property="_strings" column="value" />

So I searched Google and found this workaround (originally for a Java issue, no idea if it's suppose to work in C#)

所以我搜索了谷歌并找到了这个解决方法(最初是针对 Java 问题,不知道它是否可以在 C# 中工作)

<result property="_strings" resultMapping="someMapping.StringList"/>

<resultMap id="StringList" class="System.String">
  <result property="" column="Value"/>
</resultMap>

This at least lets the test run, and it returns the rest of my object fine, and my list has the right number of entries, except they're all blank.

这至少可以让测试运行,并且它返回我对象的其余部分,并且我的列表具有正确数量的条目,除了它们都是空白的。

I think the problem is that the property attribute is blank, but I'm not sure whats suppose to go there. (I also tried using 'value', but that didn't work either). This seems like it should be a lot simpler and I'm just overlooking something obvious.

我认为问题在于 property 属性为空,但我不确定应该去那里。(我也尝试使用“价值”,但这也不起作用)。这似乎应该简单得多,我只是忽略了一些明显的东西。

Thanks.

谢谢。

采纳答案by Brandon

Since no solution was found I just went with a method I'm not particularly proud of.

由于没有找到解决方案,我只是采用了一种我并不特别引以为豪的方法。

I mapped a class that had no other property other than a string value.

我映射了一个除了字符串值之外没有其他属性的类。

public class StringValue
{
    public String Name { get; set; }
}

<resultMap id="StringList" class="StringValue" >
  <result property="Name" column="Value"/>
</resultMap>

iBatis seems to have no problem with that, just with mapping to a collection of strings.

iBatis 似乎没有问题,只是映射到字符串集合。

回答by Brandon

At least in iBATIS3 for Java your above could just use a resultMap like:

至少在 Java 的 iBATIS3 中,你上面可以只使用像这样的 resultMap:

<resultMap id="someClassMap" type="SomeClass"> 
    <collection property="Strings" ofType="String"/> 
</resultMap>

回答by inanutshellus

My experience is the with Java version of iBATIS, but the solution should still work for the C# peeps out there.

我的经验是使用 Java 版本的 iBATIS,但该解决方案仍然适用于 C# peeps。

Given a class

给定一个班级

class MyClass {
  int id;
  List<String> firstName;
}

You can populate the list of strings or other simple types (classes without attributes, such as Integer, String, etc.) with the following two resultMaps

您可以使用以下两个 resultMap 填充字符串列表或其他简单类型(没有属性的类,例如 Integer、String 等)

<sqlMap namespace="ns">
  <resultMap id="ListMap" class="string">
    <result property="firstName" column="firstName" 
            javaType="java.util.List" jdbcType="VARCHAR"/>
  </resultMap>

  <resultMap id="PrimaryMap" class="MyClass" groupBy="id">
    <result property="id" columnName="id"/>
    <result property="firstname" resultMap="ns.ListMap" javaType="java.util.List"/>
  </resultMap>

  <select id="MySuperQuery" resultMap="PrimaryMap">
    select id, firstName from user
  </select>
</sqlMap>

Hope this helps.

希望这可以帮助。

回答by rahul parashar

you can just use 'System.String' or 'java.lang.String' as the property

你可以只使用 'System.String' 或 'java.lang.String' 作为属性

回答by user18943

Use auto result-mapping of IBatis. This is the solution in Java which you can easily map to C#. This is your sql map:

使用 IBatis 的自动结果映射。这是 Java 中的解决方案,您可以轻松地将其映射到 C#。这是你的 sql 映射:

<sqlMap namespace="Users">
<select id="names" resultClass="java.lang.String">
        select first_name as firstName from user
</select>
<sqlMap>

And then you can call it like this:

然后你可以这样称呼它:

List<String> userNames = (List<String>)sqlMap.queryForList("Users.names");

So you don't have to create a custom type with one property to do that.

因此,您不必创建具有一个属性的自定义类型来执行此操作。

回答by MalsR

The following is my experience with Java version of IBatis (version 2.3.4). My scenario was I wanted Ibatis to return me a map of keys and values for a given list of parameters. Done using Ibatis queryForMap method to return a map where the key is an Object and the values are a collection of Objects (this example Key is a Wrapper while the values are a list of Wrapper Longs).

以下是我对Java版IBatis(2.3.4版)的体验。我的场景是我希望 Ibatis 为我返回给定参数列表的键和值映射。使用 Ibatis queryForMap 方法返回一个映射,其中键是一个对象,值是一个对象的集合(这个例子 Key 是一个 Wrapper 而值是 Wrapper Longs 的列表)。

Create a placeholder (with the getters/setters) to hold the data when the query executes.

创建一个占位符(带有 getter/setter)以在查询执行时保存数据。

class PlaceHolder {
  private long elementId;;
  private List<Long> valueIds;
}

Ibatis resultmap definitions

Ibatis 结果映射定义

<resultMap id="valueIdsMap" class="java.lang.Long">
    <result property="valueIds" column="otherId" javaType="java.util.List" jdbcType="NUMERIC"/>
</resultMap>

<resultMap id="testKeysAndValuesMap" groupBy="elementId" class="PlaceHolder">
    <result property="elementId" column="elementId" jdbcType="NUMERIC" javaType="java.lang.Long"/>
  <result property="valueIds" resultMap="MapName.valueIdsMap" javaType="java.util.List" />
</resultMap>

<select id="retrieveTestKeysAndValuesMap" resultMap="testKeysAndValuesMap" 
        parameterClass="java.util.List">
    SELECT
    table_name_1.column_fk as elementId,
    table_name_1.id as otherId
    FROM table_name_1
    WHERE table_name_1.column_fk IN
        <iterate open="(" close=")" conjunction=", ">
            #[]#
        </iterate>

My initial troubles was getting the alias' right and the groupBy syntax on the parent map. Having the groupBy will get Ibatis to get the same object for the elementId to populate the children. One instance without the groupBy I found that for each key the previous child added to the list was replaced by the latest child as a new list was initialized (note I have not had a peep at the internals of Ibatis yet as I write this example). The placeholders alias must match the parent and child's resultMap. Ibatis 3 seems to have better syntax to define and handle a scenario as above.

我最初的麻烦是在父映射上获得正确的别名和 groupBy 语法。拥有 groupBy 将使 Ibatis 为 elementId 获取相同的对象来填充子项。在没有 groupBy 的一个实例中,我发现对于每个键,添加到列表中的前一个子项在初始化新列表时被最新的子项替换(注意,在我编写此示例时,我还没有窥视 Ibatis 的内部结构) . 占位符别名必须与父级和子级的 resultMap 匹配。Ibatis 3 似乎有更好的语法来定义和处理上述场景。

回答by gannu_lee

if IBatis-3, just use below

如果是 IBatis-3,请在下面使用

<select id="getFilteredList" resultType="String"> your sql here</select>

回答by prashanth yet

This worked for me , got a list of strings from procedure output cursor

这对我有用,从过程输出游标中得到了一个字符串列表

List ss = sqlmap.queryList(..

<resultMap id="emailsMap" class="java.lang.String">
        <result  column="E_MAIL" property="" /> 
</resultMap>

<parameterMap id="xp" class="java.util.Map">
    <parameter property="dd" jdbcType="VARCHAR" mode="IN" />
    <parameter property="outPutCursor" javaType="java.sql.ResultSet" jdbcType="ORACLECURSOR"  mode="OUT" />
    </parameterMap>
    enter code here

<procedure id="xx" parameterMap="xp" resultMap="emailsMap">
        { call aaa.bbb(?, ?) }
</procedure>