<?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="org.hswebframework.web.dao.crud.TestDao">

    <resultMap id="TestResultMap" type="org.hswebframework.web.dao.crud.TestEntity">
        <id property="id" column="id" javaType="Long" jdbcType="INTEGER"/>

    </resultMap>

    <resultMap id="TestNestResultMap" type="org.hswebframework.web.dao.crud.TestEntity">
        <id property="id" column="id" javaType="Long" jdbcType="INTEGER"/>
        <result property="nest.name" column="nest_table.name" javaType="String" jdbcType="VARCHAR"/>
    </resultMap>

    <!--用于动态生成sql所需的配置-->
    <sql id="config">
        <bind name="resultMapId" value="'TestResultMap'"/>
        <bind name="tableName" value="'h_test'"/>
    </sql>

    <insert id="insert" keyColumn="id" keyProperty="id" useGeneratedKeys="true" parameterType="org.hswebframework.web.dao.crud.TestEntity">
        <include refid="config"/>
        <include refid="BasicMapper.buildInsertSql"/>
    </insert>

    <update id="update" parameterType="org.hswebframework.web.commons.entity.Entity">
        <include refid="config"/>
        <include refid="BasicMapper.buildUpdateSql"/>
    </update>

    <select id="query" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="TestResultMap">
        <include refid="config"/>
        <include refid="BasicMapper.buildSelectSql"/>
    </select>

    <select id="count" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
        <include refid="config"/>
        <include refid="BasicMapper.buildTotalSql"/>
    </select>

    <select id="queryNest" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="TestNestResultMap">
        <bind name="tableName" value="'h_test'"/>
        <bind name="resultMapId" value="'TestNestResultMap'"/>
        select
        <include refid="BasicMapper.buildSelectField"/>
        from h_test
        left join h_nest_table nest_table on nest_table.id=h_test.id
        <where>
            <include refid="BasicMapper.buildWhere"/>
        </where>
        <include refid="BasicMapper.buildSortField"/>
    </select>

    <select id="countNest" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
        <include refid="config"/>
        select
        count(1)
        from h_test
        left join h_nest_table nest_table on nest_table.id=h_test.id
        <where>
            <include refid="BasicMapper.buildWhere"/>
        </where>
    </select>

</mapper>