myibatis 一对多配置

myibatis 一对多配置的配置比较简单,需要resultMap 和collection  标签,二步就能搞定。

第一步:实体类

public class LampDeviceList extends DataEntity<LampDeviceList> {

private static final long serialVersionUID = 1L;

private String gatewayId;// gateway_id

private String gatewayName;// gateway_name

private List<LampDevice> deviceList = Lists.newArrayList();

public String getGatewayId() {
return gatewayId;
}
public void setGatewayId(String gatewayId) {
this.gatewayId = gatewayId;
}
public String getGatewayName() {
return gatewayName;
}
public void setGatewayName(String gatewayName) {
this.gatewayName = gatewayName;
}
public List<LampDevice> getDeviceList() {
return deviceList;
}
public void setDeviceList(List<LampDevice> deviceList) {
this.deviceList = deviceList;
}


}

关联的实体

public class LampDeviceList extends DataEntity<LampDeviceList> {

private static final long serialVersionUID = 1L;

private String gatewayId;// gateway_id

private String gatewayName;// gateway_name

private List<LampDevice> deviceList = Lists.newArrayList();

public String getGatewayId() {
return gatewayId;
}
public void setGatewayId(String gatewayId) {
this.gatewayId = gatewayId;
}
public String getGatewayName() {
return gatewayName;
}
public void setGatewayName(String gatewayName) {
this.gatewayName = gatewayName;
}
public List<LampDevice> getDeviceList() {
return deviceList;
}
public void setDeviceList(List<LampDevice> deviceList) {
this.deviceList = deviceList;
}


}

第二步:myibatis配置resultMap 

<resultMap type="com.thinkgem.jeesite.modules.lamp.entity.LampDeviceList" id="lampDeviceList">
   <id property="id" column="id"/>
        <result property="gatewayId" column="gateway_id" />
        <result property="gatewayName" column="gateway_name" />
        
        <collection  property="deviceList"  ofType="com.thinkgem.jeesite.modules.lamp.entity.LampDevice">
            <id property="id" column="deviceList.id"/>
            <result property="deviceId" column="device_id" />
            <result property="deviceName" column="device_name" />
            <result property="shortName" column="short_name" />
            <result property="deviceMac" column="device_mac" />
            <result property="productId" column="product_id" />
            <result property="meshName" column="mesh_name" />
            <result property="factoryId" column="factory_id" />  
        </collection>
    </reresultMap >
<select id="findDevicesList" resultMap="lampDeviceList">
SELECT 
a.id AS "deviceList.id",
a.device_id AS "deviceId",
a.device_name AS "deviceName",
a.short_name AS "shortName",
a.device_mac AS "deviceMac",
a.product_id AS "productId",
a.factory_id AS "factoryId",
a.mesh_name AS "meshName",
g.id AS "id",
g.gateway_id AS "gatewayId",
g.gateway_name AS "gatewayName"

FROM lamp_gateway g
LEFT JOIN lamp_device a ON g.gateway_id = a.gateway_id
<where>
a.del_flag = #{DEL_FLAG_NORMAL}
</where>
<choose>
<when test="page !=null and page.orderBy != null and page.orderBy != ''">
ORDER BY ${page.orderBy}
</when>
<otherwise>
ORDER BY a.update_date DESC
</otherwise>
</choose>
</select>


说明:select查询的字段别名需要和resultMap里面的属性对应,同时不同有相同的别名。 

假设我们有两个表,一个是 `order` 表,一个是 `order_item` 表,每个订单可能有多个订单项。下面是 MyBatis 配置文件的代码示例: 首先,我们需要定义两个对应的实体类 `Order` 和 `OrderItem`: ```java public class Order { private int id; private String orderNo; private List<OrderItem> orderItems; // getters and setters } public class OrderItem { private int id; private String itemName; private int orderId; // getters and setters } ``` 然后,我们需要在 MyBatis 的映射文件中定义 SQL 语句和映射关系。假设我们要查询订单及其所有订单项,我们可以这样写: ```xml <!-- 定义查询订单及其所有订单项的 SQL 语句 --> <select id="selectOrderWithItems" resultMap="orderWithItems"> SELECT o.id, o.order_no, oi.id AS item_id, oi.item_name, oi.order_id FROM orders o LEFT JOIN order_items oi ON o.id = oi.order_id WHERE o.id = #{id} </select> <!-- 定义结果集映射关系 --> <resultMap id="orderWithItems" type="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <!-- 嵌套结果集映射关系 --> <collection property="orderItems" ofType="OrderItem"> <id property="id" column="item_id"/> <result property="itemName" column="item_name"/> </collection> </resultMap> ``` 在这个示例中,我们定义了一个名为 `selectOrderWithItems` 的查询语句,它会返回订单及其所有订单项的详细信息。我们使用了 `LEFT JOIN` 操作将订单表和订单项表连接起来,并使用 `WHERE` 子句限制查询结果只包含指定订单的信息。 接下来,我们定义了一个名为 `orderWithItems` 的结果集映射关系,它指定了如何将查询结果映射到 `Order` 对象和 `OrderItem` 对象中。在这个映射关系中,我们使用了 `<collection>` 元素嵌套了一个订单项的结果集映射关系,以便将订单项列表映射到 `Order` 对象的 `orderItems` 属性中。 最后,在 Java 代码中调用查询语句并获取结果: ```java public Order selectOrderWithItems(int id) { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class); return orderMapper.selectOrderWithItems(id); } } ``` 在上面的代码中,我们使用了 `SqlSession` 对象和 `OrderMapper` 接口来执行查询操作。查询结果会自动映射到 `Order` 对象中,包括所有订单项信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值