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

我与影子孤独终老i 提交于 2019-12-04 08:13:10

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.

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.

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>

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.

if IBatis-3, just use below

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

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.

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

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!