【问题标题】:Dynamic query based on column value in select statement of Ibatis?Ibatis的select语句中基于列值的动态查询?
【发布时间】:2011-07-18 07:18:01
【问题描述】:

我需要在 ibatis 中有继承映射。在Ibatis文档中,我学到的是,我们可以比较列的值来调用resultMap的discriminator标签内的子图,如下所示:

<resultMap id="map">
<discriminator javaType="java.lang.Integer" column="type">
<subMap resultMap="submap1" value="1" />
<subMap resultMap="submap2" value="2"/>
<subMap resultMap="submap3" value="3"/>
</discriminator>
</resultMap>

在上面的结果图中,子图将列映射到不同的对象。

但我想比较 select 语句本身中列的值,以便获得所需的列。一些类似如下:

<select id="load" resultMap="map">
select mt.id,mt.name, mt.type
<here we have to check the value of type column returned dynamically>
<if type = "1">
table1.column1, table1.column2 ... table1.columnN 
</if>
<if type = "2">
table2.column1, table2.column2 ... table2.columnN 
</if>
</here>
from main_table mt
LEFT OUTER JOIN TABLE1 table1 ON mt.id=table1
LEFT OUTER JOIN TABLE2 table2 ON mt.id=table2
where mt.id=#value#
</select>

这只是满足我要求的算法。在 Ibatis 中可以吗?

希望你能理解我的问题。如果问题不清楚,我很乐意重新编辑以使您清楚。

提前致谢。

【问题讨论】:

    标签: dynamic ibatis


    【解决方案1】:

    你可以试试下面的方法吗?

    <select id="load" resultMap="map">
         select mt.id,mt.name, mt.type
         <if type = "1">
             table1.column1, table1.column2 ... table1.columnN 
         </if>
         <if type = "2">
             table2.column1, table2.column2 ... table2.columnN 
         </if>
         from main_table mt
         <if type = "1">
             LEFT OUTER JOIN TABLE1 table1 ON mt.id=table1
         </if>
         <if type = "2">
             LEFT OUTER JOIN TABLE2 table2 ON mt.id=table2
         </if>
         where mt.id=#{value}
    </select>
    

    如果您在上述方式中遇到一些错误,请回复...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2013-11-27
      相关资源
      最近更新 更多