spring-boot-realworld-examp.../src/main/resources/mapper/ArticleMapper.xml

82 lines
3.0 KiB
XML
Raw Normal View History

2017-08-15 09:47:18 +07:00
<?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" >
2017-08-18 16:08:27 +07:00
<mapper namespace="io.spring.infrastructure.mybatis.mapper.ArticleMapper">
2017-08-15 09:47:18 +07:00
<insert id="insert">
insert into articles(id, slug, title, description, body, user_id, created_at, updated_at)
values(
#{article.id},
#{article.slug},
#{article.title},
#{article.description},
#{article.body},
#{article.userId},
#{article.createdAt},
#{article.updatedAt})
</insert>
<insert id="insertTag">
insert into tags (id, name) values (#{tag.id}, #{tag.name})
</insert>
<insert id="insertArticleTagRelation">
insert into article_tags (article_id, tag_id) values(#{articleId}, #{tagId})
</insert>
2017-08-15 13:17:54 +07:00
<update id="update">
update articles
<set>
<if test="article.title != ''">title = #{article.title},</if>
<if test="article.title != ''">slug = #{article.slug},</if>
<if test="article.description != ''">description = #{article.description},</if>
<if test="article.body != ''">body = #{article.body}</if>
</set>
where id = #{article.id}
</update>
2017-08-15 13:35:29 +07:00
<delete id="delete">
delete from articles where id = #{id}
</delete>
2017-08-15 13:17:54 +07:00
<sql id="selectArticle">
2017-08-15 09:47:18 +07:00
select
2017-08-15 13:17:54 +07:00
A.id articleId,
A.slug articleSlug,
A.title articleTitle,
A.description articleDescription,
A.body articleBody,
A.user_id articleUserId,
A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt,
T.id tagId,
T.name tagName
from articles A
left join article_tags AT on A.id = AT.article_id
left join tags T on T.id = AT.tag_id
</sql>
<select id="findById" resultMap="article">
<include refid="selectArticle"/>
2017-08-15 09:47:18 +07:00
where A.id = #{id}
</select>
<select id="findTag" resultType="io.spring.core.article.Tag">
select id, name from tags where name = #{tagName}
2017-08-15 09:47:18 +07:00
</select>
2017-08-15 13:17:54 +07:00
<select id="findBySlug" resultMap="article">
<include refid="selectArticle"/>
where A.slug = #{slug}
</select>
2017-08-15 09:47:18 +07:00
<resultMap id="article" type="io.spring.core.article.Article">
<id column="articleId" property="id"/>
<result column="articleUserId" property="userId"/>
<result column="articleTitle" property="title"/>
<result column="articleSlug" property="slug"/>
<result column="articleDescription" property="description"/>
<result column="articleBody" property="body"/>
<result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/>
<collection property="tags" ofType="arraylist" resultMap="tag"/>
</resultMap>
<resultMap id="tag" type="io.spring.core.article.Tag">
<id column="tagId" property="id"/>
<result column="tagName" property="name"/>
</resultMap>
</mapper>