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里面的属性对应,同时不同有相同的别名。 

猜你喜欢

转载自blog.csdn.net/qsky001/article/details/77506731
今日推荐