How do you map a List in iBatis?

2020-07-13 07:22发布

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.

8条回答
迷人小祖宗
2楼-- · 2020-07-13 07:35

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>
查看更多
再贱就再见
3楼-- · 2020-07-13 07:37

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

查看更多
不美不萌又怎样
4楼-- · 2020-07-13 07:40

if IBatis-3, just use below

<select id="getFilteredList" resultType="String"> your sql here</select>
查看更多
趁早两清
5楼-- · 2020-07-13 07:42

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.

查看更多
冷血范
6楼-- · 2020-07-13 07:44

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.

查看更多
等我变得足够好
7楼-- · 2020-07-13 07:47

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.

查看更多
登录 后发表回答