简体   繁体   English

如何在ibatis和Java中处理多个表的sql映射

[英]How do you handle sql mapping for multiple tables in ibatis and java

I am trying to use Ibatis with GWT and I have this scenario, I have databases tables Airport, Terminals and Flights. 我正在尝试将Ibatis与GWT一起使用,并且遇到这种情况,我有数据库表Airport,Terminals和Flights。 An airport can have different terminals. 机场可以有不同的航站楼。 A terminal can have one Airport and many flights. 一个航站楼可以有一个机场和多个航班。 And a flight can have one terminal. 一个航班可以有一个航站楼。 So the table structure looks like this. 因此表结构如下所示。

Airport -id -name -terminal_id 机场-id-名称-terminal_id

Terminals -id -name -flight_id 终端-id -name -flight_id

Flights -id -airline -terminal_id 航班-id-航空公司-terminal_id

My select statement looks like this 我的选择语句看起来像这样

SELECT airport.name AS Airport,
       terminals.name AS Terminal,
       flights.airline,
FROM airport,
     terminals,
     flights
WHERE airport.terminal_id = terminals.id
  AND terminals.flight_id = flights.id;

What will the sql maps look like to get this result. sql映射将如何获得此结果。 Where I'm getting confused is the result set is a combination of tables and so the result set isn't a model object of either of the three tables. 我感到困惑的是结果集是表的组合,因此结果集不是这三个表中任何一个的模型对象。

Create a custom value object(vo) to suit your need. 创建一个自定义值对象(vo)以适合您的需求。

<sqlMap namespace="Arrival">
<resultMap id="Arrival" class="com.flight.vo.Arrival">
    <result property="airport" column="Airport" />
    <result property="terminal" column="Terminal" />
    <result property="airline" column="airline"/>
</resultMap>

<select id="retrieveAllArrivals" resultMap="Arrival.Arrival" >
    select airport.name as Airport, terminals.name as Terminal, flights.airline
    FROM airport, terminals, flights 
    WHERE airport.terminal_id = terminals.id 
    AND terminals.flight_id = flights.id
</select>
</sqlMap>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM