ArticleMapper.xml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.dbs.realworld.mapper.ArticleMapper">
  4. <insert id="insertArticle" parameterType="ArticleDTO" useGeneratedKeys="true" keyProperty="id">
  5. <![CDATA[
  6. INSERT
  7. INTO article_mst
  8. ( user_id, title, subtitle, content, create_datetime, tags )
  9. VALUES ( #{writerId}, #{title}, #{subtitle}, #{content}, #{created}, #{tags} )
  10. ]]>
  11. </insert>
  12. <update id="updateArticle" parameterType="ArticleDTO">
  13. <![CDATA[
  14. UPDATE article_mst
  15. SET update_datetime = NOW()
  16. ]]>
  17. <if test='title != null'>
  18. , title = #{title}
  19. </if>
  20. <if test='subtitle != null'>
  21. , subtitle = #{subtitle}
  22. </if>
  23. <if test='content != null'>
  24. , content = #{content}
  25. </if>
  26. <if test='tags != null'>
  27. , tags = #{tags}
  28. </if>
  29. <![CDATA[
  30. WHERE id = #{id}
  31. ]]>
  32. </update>
  33. <!--
  34. 커서 기반 페이징
  35. - articleId는 커서 역할
  36. - -1은 초깃값이며 이 경우 최초의 페이지 데이터들을 조회
  37. - feed는 현재 사용자가 보고 있는 feed
  38. - 'your-feed' 혹은 'global-feed' 값을 가지며 기본 값은 'global-feed'
  39. - 시간별 최신 정렬 및 페이지 당 10개의 데이터 조회
  40. -->
  41. <select id="select" parameterType="hashmap" resultType="ArticleDTO">
  42. <![CDATA[
  43. SELECT
  44. AR.id AS id,
  45. AR.title AS title,
  46. AR.subtitle AS subtitle,
  47. AR.content AS content,
  48. AR.tags AS tags,
  49. AR.create_datetime AS created,
  50. AR.favorite_num AS favoriteNum,
  51. ]]>
  52. <if test='userId != -1'>
  53. <![CDATA[
  54. (
  55. SELECT COUNT(*) FROM article_like AL
  56. WHERE AL.user_id = #{userId}
  57. AND AL.article_id = AR.id
  58. ) AS favorite,
  59. ]]>
  60. </if>
  61. <![CDATA[
  62. AR.user_id AS writerId,
  63. USR.name AS writerName,
  64. USR.email AS writerEmail
  65. FROM article_mst AR
  66. LEFT JOIN user_mst USR
  67. ON AR.user_id = USR.id
  68. WHERE 1=1
  69. ]]>
  70. <if test='articleId != -1'>
  71. <![CDATA[
  72. AND AR.id < #{articleId}
  73. ]]>
  74. </if>
  75. <if test='feed == "your-feed"'>
  76. <![CDATA[
  77. AND AR.user_id = #{userId}
  78. ]]>
  79. </if>
  80. <![CDATA[
  81. ORDER BY create_datetime DESC
  82. LIMIT 10
  83. ]]>
  84. </select>
  85. <select id="selectPagingInfo" parameterType="hashmap" resultType="hashmap">
  86. <![CDATA[
  87. SELECT
  88. COUNT(*) AS total,
  89. COUNT( IF(id < #{articleId}, 1, null) ) AS nextItemNum
  90. FROM article_mst AR
  91. ]]>
  92. <if test='feed == "your-feed"'>
  93. <![CDATA[
  94. WHERE AR.user_id = #{userId}
  95. GROUP BY AR.user_id
  96. ]]>
  97. </if>
  98. </select>
  99. <select id="selectAll" parameterType="ArticleDTO" resultType="ArticleDTO">
  100. <![CDATA[
  101. SELECT
  102. AR.id AS id,
  103. AR.title AS title,
  104. AR.subtitle AS subtitle,
  105. AR.content AS content,
  106. AR.tags AS tags,
  107. AR.create_datetime AS created,
  108. AR.user_id AS writerId,
  109. USR.name AS writerName,
  110. USR.email AS writerEmail
  111. FROM article_mst AR
  112. LEFT JOIN user_mst USR
  113. ON AR.user_id = USR.id
  114. ORDER BY create_datetime DESC
  115. LIMIT 10
  116. ]]>
  117. </select>
  118. <select id="selectByArticleId" parameterType="int" resultType="ArticleDTO">
  119. <![CDATA[
  120. SELECT
  121. AR.id AS id,
  122. AR.title AS title,
  123. AR.subtitle AS subtitle,
  124. AR.content AS content,
  125. AR.tags AS tags,
  126. AR.create_datetime AS created,
  127. AR.user_id AS writerId,
  128. USR.name AS writerName,
  129. USR.email AS writerEmail
  130. FROM article_mst AR
  131. LEFT JOIN user_mst USR
  132. ON AR.user_id = USR.id
  133. WHERE AR.id = #{articleId}
  134. ]]>
  135. </select>
  136. <delete id="deleteByArticleId" parameterType="int">
  137. <![CDATA[
  138. DELETE FROM article_mst WHERE id = #{articleId}
  139. ]]>
  140. </delete>
  141. <insert id="insertFavorite" parameterType="hashmap">
  142. <![CDATA[
  143. INSERT IGNORE INTO article_like
  144. ( user_id, article_id )
  145. VALUES
  146. ( #{userId}, #{articleId} )
  147. ]]>
  148. </insert>
  149. <delete id="deleteFavorite" parameterType="hashmap">
  150. <![CDATA[
  151. DELETE FROM article_like
  152. WHERE user_id = #{userId}
  153. AND article_id = #{articleId}
  154. ]]>
  155. </delete>
  156. <update id="updateFavoriteNum" parameterType="int">
  157. <![CDATA[
  158. UPDATE article_mst
  159. SET favorite_num = (SELECT COUNT(*) FROM article_like WHERE article_id = #{articleId})
  160. WHERE id = #{articleId}
  161. ]]>
  162. </update>
  163. </mapper>