<?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.javaclimb.music.dao.SongMapper">
    <resultMap id="BaseResultMap" type="com.javaclimb.music.domain.Song" >
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="singer_id" jdbcType="VARCHAR" property="singerId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="introduction" jdbcType="VARCHAR" property="introduction"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="pic" jdbcType="VARCHAR" property="pic"/>
        <result column="url" jdbcType="VARCHAR" property="url"/>
    </resultMap>

    <resultMap id="ResultMapWithBLOBs" type="com.javaclimb.music.domain.Song" extends="BaseResultMap">
        <result column="lyric" jdbcType="LONGVARCHAR" property="lyric"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,singer_id,name,introduction,create_time,update_time,pic,url
    </sql>

    <sql id="Blob_Column_List">
        id,singer_id,name,introduction,create_time,update_time,pic,lyric,url
    </sql>

    <insert id="insert" parameterType="com.javaclimb.music.domain.Song">
        insert into song
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="singerId != null">
                singer_id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="introduction != null">
                introduction,
            </if>
            create_time,update_time,
            <if test="pic != null">
                pic,
            </if>
            <if test="lyric != null">
                lyric,
            </if>
            <if test="url != null">
                url,
            </if>
        </trim>
        <trim prefix=" values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="singerId != null">
                #{singerId},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="introduction != null">
                #{introduction},
            </if>
            now(),now(),
            <if test="pic != null">
                #{pic},
            </if>
            <if test="lyric != null">
                #{lyric},
            </if>
            <if test="url != null">
                #{url},
            </if>
        </trim>        
    </insert>
    
    <update id="update" parameterType="com.javaclimb.music.domain.Song">
        update song
        <set>
            <if test="singerId != null">
                singer_id = #{singerId},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="introduction != null">
                introduction = #{introduction},
            </if>
            update_time = now(),
            <if test="pic != null">
                pic = #{pic},
            </if>
            <if test="lyric != null">
                lyric = #{lyric},
            </if>
            <if test="url != null">
                url = #{url},
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from song
        where id=#{id}
    </delete>

    <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
        select
        <include refid="Blob_Column_List"/>
        from song
        where id=#{id}
    </select>

    <select id="allSong" resultMap="ResultMapWithBLOBs">
        select
        <include refid="Blob_Column_List"/>
        from song
    </select>

    <select id="songOfName" resultMap="ResultMapWithBLOBs" parameterType="java.lang.String">
        select
        <include refid="Blob_Column_List"/>
        from song
        where name = #{name}
    </select>

    <select id="likeSongOfName" resultMap="ResultMapWithBLOBs" parameterType="java.lang.String">
        select
        <include refid="Blob_Column_List"/>
        from song
        where name like #{name}
    </select>

    <select id="songOfSingerId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
        select
        <include refid="Blob_Column_List"/>
        from song
        where singer_id = #{singerId}
    </select>

</mapper>
















