Ibatis的select语句根据列值进行动态查询?

发布于 2024-11-24 20:46:34 字数 1047 浏览 3 评论 0原文

我需要在 ibatis 中进行继承映射。在Ibatis文档中,我了解到,我们可以比较列的值来调用resultMap的鉴别器标记内的子图,如下所示:

<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中可以吗?

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

提前致谢。

I need to have inheritance mapping in ibatis. In Ibatis documentation, what I have learned is, we can compare the value of the column to call submaps inside discriminator tag of resultMap like as follows:

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

In the above resultmap, the submaps maps the columns to different object.

But I want to compare the value of column in select statement itself so that I can get the required columns. some what like as follows:

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

This is just an algorithm for my requirement. Is it possible in Ibatis?

Hope you understand my problem. If the question is not clear, I am happy to reedit this to make you clear.

Thanks in advance.

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

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

发布评论

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

评论(1

北城挽邺 2024-12-01 20:46:34

您可以尝试以下方式吗?

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

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

You could try in following way?

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

If you face some error in above way please reply...

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