<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tofly.water.mapper.DeviceBusinessMapper">

    <resultMap type="com.tofly.water.entity.DeviceBusiness" id="DeviceBusinessMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="businessId" column="BUSINESS_ID" jdbcType="INTEGER"/>
        <result property="outsideTable" column="OUTSIDE_TABLE" jdbcType="VARCHAR"/>
        <result property="createTime" column="CREATE_TIME" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="UPDATE_TIME" jdbcType="TIMESTAMP"/>
        <association property="business" column="{businessId = BUSINESS_ID,outsideTable = OUTSIDE_TABLE}" select="selectByBus"></association>
    </resultMap>

    <select id="selectByBus" resultType="map">
        select * from ${outsideTable} where id = #{businessId}
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into TF_DEVICE_BUSINESS(BUSINESS_ID, OUTSIDE_TABLE, CREATE_TIME, UPDATE_TIME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.businessId}, #{entity.outsideTable}, #{entity.createTime}, #{entity.updateTime})
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into TF_DEVICE_BUSINESS(BUSINESS_ID, OUTSIDE_TABLE, CREATE_TIME, UPDATE_TIME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.businessId}, #{entity.outsideTable}, #{entity.createTime}, #{entity.updateTime})
        </foreach>
        on duplicate key update
        BUSINESS_ID = values(BUSINESS_ID) , OUTSIDE_TABLE = values(OUTSIDE_TABLE) , CREATE_TIME = values(CREATE_TIME) ,
        UPDATE_TIME = values(UPDATE_TIME)
    </insert>

</mapper>

