<?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.forum.dao.TagDao">


    <select id="getTag" resultType="Tag">
        select id,name from tag where id = #{id}
    </select>

    <insert id="saveTag" parameterType="Tag">
        insert into tag values (#{id},#{name});
    </insert>

    <!--两个id可能会相冲，取别名即可-->
    <resultMap id="tags" type="Tag">
        <id property="id" column="tid"/>
        <result property="name" column="name"/>
        <collection property="forums" ofType="Forum">
            <id property="id" column="bid"/>
            <result property="title" column="title"/>
        </collection>
    </resultMap>

    <select id="getAllTag" resultType="com.forum.entity.Tag">
        select * from tag
    </select>

    <select id="getForumTag" resultMap="tags">
        select t.id tid, t.name, b.id bid, b.title
        from tag t, forum b, forum_tags bt
        where t.id = bt.tag_id and b.id = bt.forum_id
    </select>

    <select id="getTagByName" resultType="Tag">
        select * from tag where name = #{name}
    </select>
    <select id="getTagById" resultType="Tag">
        select * from tag where id = #{id}
    </select>
    <select id="countTag" resultType="java.lang.Integer">
        select count(*) from tag
    </select>

    <delete id="deleteTag">
        delete from tag where id = #{id}
    </delete>

    <update id="updateTag" parameterType="Tag">
        update tag set name = #{name} where id = #{id};
    </update>

</mapper>
