<?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.hchyun.test.dao.TestTreeDao">
    
    <resultMap type="TestTree" id="TestTreeResult">
        <result property="id"    column="id"    />
        <result property="pId"    column="p_id"    />
        <result property="content"    column="content"    />
    </resultMap>

    <sql id="selectTestTreeVo">
        select id, p_id, content from test_tree
    </sql>

    <select id="selectTestTreeList" parameterType="TestTree" resultMap="TestTreeResult">
        <include refid="selectTestTreeVo"/>
        <where>  
            <if test="content != null  and content != ''"> and content like concat('%', #{content}, '%')</if>
        </where>
    </select>
    
    <select id="selectTestTreeById" parameterType="Long" resultMap="TestTreeResult">
        <include refid="selectTestTreeVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTestTree" parameterType="TestTree" useGeneratedKeys="true" keyProperty="id">
        insert into test_tree
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pId != null">p_id,</if>
            <if test="content != null and content != ''">content,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pId != null">#{pId},</if>
            <if test="content != null and content != ''">#{content},</if>
         </trim>
    </insert>

    <update id="updateTestTree" parameterType="TestTree">
        update test_tree
        <trim prefix="SET" suffixOverrides=",">
            <if test="pId != null">p_id = #{pId},</if>
            <if test="content != null and content != ''">content = #{content},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTestTreeById" parameterType="Long">
        delete from test_tree where id = #{id}
    </delete>

    <delete id="deleteTestTreeByIds" parameterType="String">
        delete from test_tree where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>