如何映射 List在 iBatis 中?
我有一个像这样的类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以仅使用“System.String”或“java.lang.String”作为属性
you can just use 'System.String' or 'java.lang.String' as the property
这对我有用,从程序输出光标中获取了字符串列表
This worked for me , got a list of strings from procedure output cursor
使用 IBatis 的自动结果映射。 这是 Java 中的解决方案,您可以轻松地将其映射到 C#。
这是你的 sql 映射:
然后你可以这样称呼它:
因此,您不必创建具有一个属性的自定义类型来执行此操作。
Use auto result-mapping of IBatis. This is the solution in Java which you can easily map to C#.
This is your sql map:
And then you can call it like this:
So you don't have to create a custom type with one property to do that.
我的经验是使用 Java 版本的 iBATIS,但该解决方案应该仍然适用于 C# 爱好者。
给定一个类,
您可以使用以下两个 resultMap 填充字符串或其他简单类型(没有属性的类,例如 Integer、String 等)列表
希望这会有所帮助。
My experience is the with Java version of iBATIS, but the solution should still work for the C# peeps out there.
Given a class
You can populate the list of strings or other simple types (classes without attributes, such as Integer, String, etc.) with the following two resultMaps
Hope this helps.
至少在 iBATIS3 for Java 中,上面的内容可以只使用 resultMap,例如:
At least in iBATIS3 for Java your above could just use a resultMap like:
以下是我使用Java版IBatis(版本2.3.4)的经验。 我的场景是我希望 Ibatis 返回给定参数列表的键和值的映射。 使用 Ibatis queryForMap 方法返回一个映射,其中键是一个对象,值是对象的集合(此示例键是一个包装器,而值是包装器长整型列表)。
创建一个占位符(带有 getter/setter)以在查询执行时保存数据。
Ibatis 结果映射定义
我最初的麻烦是在父映射上获取正确的别名和 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.
Ibatis resultmap definitions
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.
如果是IBatis-3,只需使用下面的
if IBatis-3, just use below
由于没有找到解决方案,我只是采用了一种我并不特别自豪的方法。
我映射了一个除了字符串值之外没有其他属性的类。
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.
iBatis seems to have no problem with that, just with mapping to a collection of strings.