如何映射 List在 iBatis 中?

发布于 2024-07-26 19:08:09 字数 839 浏览 1 评论 0原文

我有一个像这样的类

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

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

我将如何为 _strings 进行映射?

我尝试了这个,但它抱怨找不到列表类型处理程序,如果我将其映射为对象,它就不会抱怨。

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

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

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

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

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

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

谢谢。

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?

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.

<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#)

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

Thanks.

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

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

发布评论

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

评论(8

私野 2024-08-02 19:08:10

您可以仅使用“System.String”或“java.lang.String”作为属性

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

赤濁 2024-08-02 19:08:10

这对我有用,从程序输出光标中获取了字符串列表

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>

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>
九厘米的零° 2024-08-02 19:08:09

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

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

然后你可以这样称呼它:

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

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

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

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

夏尔 2024-08-02 19:08:09

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

给定一个类,

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

您可以使用以下两个 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>

希望这会有所帮助。

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

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

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

策马西风 2024-08-02 19:08:09

至少在 iBATIS3 for Java 中,上面的内容可以只使用 resultMap,例如:

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

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

<resultMap id="someClassMap" type="SomeClass"> 
    <collection property="Strings" ofType="String"/> 
</resultMap>
明明#如月 2024-08-02 19:08:09

以下是我使用Java版IBatis(版本2.3.4)的经验。 我的场景是我希望 Ibatis 返回给定参数列表的键和值的映射。 使用 Ibatis queryForMap 方法返回一个映射,其中键是一个对象,值是对象的集合(此示例键是一个包装器,而值是包装器长整型列表)。

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

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

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>

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

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

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

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

Ibatis resultmap definitions

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

眼趣 2024-08-02 19:08:09

如果是IBatis-3,只需使用下面的

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

if IBatis-3, just use below

<select id="getFilteredList" resultType="String"> your sql here</select>
¢蛋碎的人ぎ生 2024-08-02 19:08:09

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

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

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

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

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

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.

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