<?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="<%= entityAbsolutePackage %>.repository.<%= entityClass %>Repository">

    <!-- begcode-please-regenerate-this-file 如果您不希望重新生成代码时被覆盖，将please修改为don't ！！！-->

    <!-- 通用查询映射结果 -->
    <resultMap id="baseResultMap" type="<%= entityAbsolutePackage %>.domain.<%= entityClass %>">
        <id column="id" property="id"/>
        <%_ for (idx in fields) {
            const fieldName = fields[idx].fieldName;
            const fieldNameAsDatabaseColumn = fields[idx].fieldNameAsDatabaseColumn;
        _%>
        <result column="<%= fieldNameAsDatabaseColumn %>" property="<%= fieldName %>"<% if (fields[idx].fieldType !== 'byte[]' && fields[idx].fieldType.indexOf('[]') !== -1) { %> typeHandler = "org.apache.ibatis.type.ArrayTypeHandler"<% } %> />
        <%_ } _%>
    </resultMap>
    <%_ if (extensibleData) { _%>

    <!-- 保存map形式的记录 -->
    <insert id="insertByMap">
        insert into <%= entityTableName %>
        <foreach collection="fieldsMap.keys" item="key" separator="," open="(" close=")" >
            ${key}
        </foreach>
        values
        <foreach collection="fieldsMap.values" item="value" separator="," open="(" close=")" >
            #{value}
        </foreach>
    </insert>

    <select id="selectMapByIdAndColumns" resultType="map">
        select
        <foreach collection="fieldsMap" index="key" item="value" separator="," >
            ${key} ${value}
        </foreach>
        from <%= entityTableName %>
        where id = #{id}
    </select>

    <update id="updateToManyRelationById">
        update ${tableName}
        set
        <foreach collection="fieldsMap" index="key" item="value" separator="," >
            ${key} = ${value}
        </foreach>
        where id = #{id}
    </update>

    <insert id="insertToManyRelation">
        insert ${tableName}
        <foreach collection="fieldsMap.keys" item="key" separator="," open="(" close=")" >
            ${key}
        </foreach>
        values
        <foreach collection="fieldsMap.values" item="value" separator="," open="(" close=")" >
            #{value}
        </foreach>
    </insert>

    <select id="selectMapByTableAndIdAndColumns" resultType="map">
        select
        <foreach collection="fieldsMap" index="key" item="value" separator="," >
            ${key} ${value}
        </foreach>
        from ${table} a left join ${joinTable} b on a.${relationIdName} = b.id
        where a.id = #{id}
        limit 1
    </select>

    <select id="selectMapsByTableAndIdAndColumns" resultType="map">
        select
        <foreach collection="fieldsMap" index="key" item="value" separator="," >
            ${key} ${value}
        </foreach>
        from ${joinTable} a left join ${otherTable} b on a.${otherIdName} = b.id
        where a.${relationIdName} = #{id}
    </select>
    <%_ } _%>

    <!-- 通用查询结果列 -->
    <sql id="baseColumnList">
    <%_ for (idx in fields) { _%>
        <%= fields[idx].fieldNameAsDatabaseColumn %><% if (idx < fields.length - 1) { %>,<% } %>
    <%_ } _%>
    </sql>

</mapper>
