123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?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.dbs.realworld.mapper.ArticleMapper">
-
- <insert id="insertArticle" parameterType="ArticleDTO" useGeneratedKeys="true" keyProperty="id">
- <![CDATA[
- INSERT
- INTO article_mst
- ( user_id, title, subtitle, content, create_datetime, tags )
- VALUES ( #{writerId}, #{title}, #{subtitle}, #{content}, #{created}, #{tags} )
- ]]>
- </insert>
- <update id="updateArticle" parameterType="ArticleDTO">
- <![CDATA[
- UPDATE article_mst
- SET update_datetime = NOW()
- ]]>
- <if test='title != null'>
- , title = #{title}
- </if>
- <if test='subtitle != null'>
- , subtitle = #{subtitle}
- </if>
- <if test='content != null'>
- , content = #{content}
- </if>
- <if test='tags != null'>
- , tags = #{tags}
- </if>
- <![CDATA[
- WHERE id = #{id}
- ]]>
- </update>
- <!--
- 커서 기반 페이징
- - articleId는 커서 역할
- - -1은 초깃값이며 이 경우 최초의 페이지 데이터들을 조회
- - feed는 현재 사용자가 보고 있는 feed
- - 'your-feed' 혹은 'global-feed' 값을 가지며 기본 값은 'global-feed'
- - 시간별 최신 정렬 및 페이지 당 10개의 데이터 조회
- -->
- <select id="select" parameterType="hashmap" resultType="ArticleDTO">
- <![CDATA[
- SELECT
- AR.id AS id,
- AR.title AS title,
- AR.subtitle AS subtitle,
- AR.content AS content,
- AR.tags AS tags,
- AR.create_datetime AS created,
- AR.favorite_num AS favoriteNum,
- ]]>
- <if test='userId != -1'>
- <![CDATA[
- (
- SELECT COUNT(*) FROM article_like AL
- WHERE AL.user_id = #{userId}
- AND AL.article_id = AR.id
- ) AS favorite,
- ]]>
- </if>
- <![CDATA[
- AR.user_id AS writerId,
- USR.name AS writerName,
- USR.email AS writerEmail
- FROM article_mst AR
- LEFT JOIN user_mst USR
- ON AR.user_id = USR.id
- WHERE 1=1
- ]]>
- <if test='articleId != -1'>
- <![CDATA[
- AND AR.id < #{articleId}
- ]]>
- </if>
- <if test='feed == "your-feed"'>
- <![CDATA[
- AND AR.user_id = #{userId}
- ]]>
- </if>
- <![CDATA[
- ORDER BY create_datetime DESC
- LIMIT 10
- ]]>
- </select>
- <select id="selectPagingInfo" parameterType="hashmap" resultType="hashmap">
- <![CDATA[
- SELECT
- COUNT(*) AS total,
- COUNT( IF(id < #{articleId}, 1, null) ) AS nextItemNum
- FROM article_mst AR
- ]]>
- <if test='feed == "your-feed"'>
- <![CDATA[
- WHERE AR.user_id = #{userId}
- GROUP BY AR.user_id
- ]]>
- </if>
- </select>
- <select id="selectAll" parameterType="ArticleDTO" resultType="ArticleDTO">
- <![CDATA[
- SELECT
- AR.id AS id,
- AR.title AS title,
- AR.subtitle AS subtitle,
- AR.content AS content,
- AR.tags AS tags,
- AR.create_datetime AS created,
- AR.user_id AS writerId,
- USR.name AS writerName,
- USR.email AS writerEmail
- FROM article_mst AR
- LEFT JOIN user_mst USR
- ON AR.user_id = USR.id
- ORDER BY create_datetime DESC
- LIMIT 10
- ]]>
- </select>
- <select id="selectByArticleId" parameterType="int" resultType="ArticleDTO">
- <![CDATA[
- SELECT
- AR.id AS id,
- AR.title AS title,
- AR.subtitle AS subtitle,
- AR.content AS content,
- AR.tags AS tags,
- AR.create_datetime AS created,
- AR.user_id AS writerId,
- USR.name AS writerName,
- USR.email AS writerEmail
- FROM article_mst AR
- LEFT JOIN user_mst USR
- ON AR.user_id = USR.id
- WHERE AR.id = #{articleId}
- ]]>
- </select>
- <delete id="deleteByArticleId" parameterType="int">
- <![CDATA[
- DELETE FROM article_mst WHERE id = #{articleId}
- ]]>
- </delete>
- <insert id="insertFavorite" parameterType="hashmap">
- <![CDATA[
- INSERT IGNORE INTO article_like
- ( user_id, article_id )
- VALUES
- ( #{userId}, #{articleId} )
- ]]>
- </insert>
- <delete id="deleteFavorite" parameterType="hashmap">
- <![CDATA[
- DELETE FROM article_like
- WHERE user_id = #{userId}
- AND article_id = #{articleId}
- ]]>
- </delete>
- <update id="updateFavoriteNum" parameterType="int">
- <![CDATA[
- UPDATE article_mst
- SET favorite_num = (SELECT COUNT(*) FROM article_like WHERE article_id = #{articleId})
- WHERE id = #{articleId}
- ]]>
- </update>
- </mapper>
|