PostsMapper.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.sumbytes.helloblog.posts.dao.PostsDao">
  4. <sql id="BASE_SQL">
  5. id,author_id as authorId,title,thumbnail,comments, is_comment as isComment, sync_status as syncStatus,status,summary,views,weight,category_id as categoryId,create_time as createTime,update_time as updateTime
  6. </sql>
  7. <!-- 通用查询映射结果 -->
  8. <resultMap id="BaseResultMap" type="com.sumbytes.helloblog.posts.domain.po.Posts">
  9. <id column="id" property="id"/>
  10. <result column="title" property="title"/>
  11. <result column="thumbnail" property="thumbnail"/>
  12. <result column="comments" property="comments"/>
  13. <result column="status" property="status"/>
  14. <result column="summary" property="summary"/>
  15. <result column="views" property="views"/>
  16. <result column="author_id" property="authorId"/>
  17. <result column="sync_status" property="syncStatus"/>
  18. <result column="category_id" property="categoryId"/>
  19. <result column="is_comment" property="isComment"/>
  20. <result column="weight" property="weight"/>
  21. <result column="create_time" property="createTime"/>
  22. <result column="update_time" property="updateTime"/>
  23. </resultMap>
  24. <select id="selectPostsList" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsVO">
  25. SELECT
  26. DISTINCT(posts.id),
  27. posts.title,
  28. posts.status,
  29. posts.summary,
  30. posts.thumbnail,
  31. posts.sync_status syncStatus,
  32. users.name author,
  33. posts.views,
  34. posts.comments,
  35. posts.category_id as categoryId,
  36. category.name as categoryName,
  37. posts.weight,
  38. posts.create_time createTime
  39. FROM
  40. hello_blog_posts posts
  41. LEFT JOIN hello_blog_auth_user users ON users.id = posts.author_id
  42. LEFT JOIN hello_blog_posts_tags postsTags ON postsTags.posts_id = posts.id
  43. LEFT JOIN hello_blog_tags tags ON postsTags.tags_id = tags.id
  44. LEFT JOIN hello_blog_category category ON category.id=posts.category_id
  45. <where>
  46. <if test="condition.archiveDate != null">
  47. BETWEEN DATE_FORMAT( condition.archiveDate, "%Y-%m-01 00:00:00") AND DATE_FORMAT(condition.archiveDate, "%Y-%m-01 59:59:59" )
  48. </if>
  49. <if test="condition.createTime != null">
  50. AND posts.create_time = #{condition.createTime}
  51. </if>
  52. <if test="condition.keywords != null and condition.keywords != ''">
  53. AND (posts.title LIKE #{condition.keywords} OR posts.summary LIKE #{condition.keywords})
  54. </if>
  55. <if test="condition.tagsName != null and condition.tagsName != ''">
  56. AND tags.name = #{condition.tagsName}
  57. </if>
  58. <if test="condition.categoryId != null and condition.categoryId != ''">
  59. AND posts.category_id = #{condition.categoryId}
  60. </if>
  61. <if test="condition.postsTagsId != null and condition.postsTagsId != ''">
  62. AND postsTags.tags_id = #{condition.postsTagsId}
  63. </if>
  64. <if test="condition.title != null and condition.title != ''">
  65. AND posts.title LIKE #{condition.title}
  66. </if>
  67. <if test="condition.isPublishByteBlogs != null and condition.isPublishByteBlogs != ''">
  68. AND posts.sync_status = #{condition.isPublishByteBlogs}
  69. </if>
  70. <if test="condition.status != null and condition.status != ''">
  71. AND posts.status = #{condition.status}
  72. </if>
  73. </where>
  74. <choose>
  75. <when test="condition.isWeight != null and condition.isWeight == 1" >
  76. ORDER BY posts.weight DESC
  77. </when>
  78. <otherwise>
  79. ORDER BY posts.id DESC
  80. </otherwise>
  81. </choose>
  82. </select>
  83. <select id="selectArchiveGroupYearList" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsVO">
  84. SELECT
  85. id,
  86. title,
  87. create_time createTime,
  88. DATE_FORMAT( create_time, "%Y" ) `year`
  89. FROM
  90. hello_blog_posts
  91. ORDER BY
  92. DATE_FORMAT( create_time, "%Y" ) DESC
  93. </select>
  94. <select id="selectPostsTotal" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsVO">
  95. SELECT
  96. SUM(comments) commentsTotal,
  97. SUM(views) viewsTotal
  98. FROM
  99. hello_blog_posts posts
  100. </select>
  101. <select id="selectArchiveTotalGroupDateList" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsVO">
  102. SELECT
  103. DATE_FORMAT( create_time, "%Y-%m-01 00:00:00" ) archiveDate,
  104. COUNT(*) articleTotal
  105. FROM
  106. hello_blog_posts
  107. GROUP BY DATE_FORMAT( create_time, "%Y-%m-01 00:00:00" )
  108. ORDER BY id DESC
  109. </select>
  110. <select id="selectOneById" resultType="com.sumbytes.helloblog.posts.domain.po.Posts">
  111. SELECT
  112. posts.`id`,
  113. posts.`author_id`,
  114. posts.`title`,
  115. posts.`thumbnail`,
  116. posts.`comments`,
  117. posts.`is_comment`,
  118. posts.`sync_status`,
  119. posts.`status`,
  120. posts.`summary`,
  121. posts.`views`,
  122. posts.`weight`,
  123. posts.`category_id`,
  124. category.name as categoryName,
  125. posts.`create_time`,
  126. posts.`update_time`
  127. FROM hello_blog_posts as posts
  128. LEFT JOIN hello_blog_category as category ON category.id=posts.category_id
  129. WHERE posts.id=#{id}
  130. </select>
  131. <select id="selectByArchiveDate" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsVO">
  132. SELECT <include refid="BASE_SQL"/>
  133. FROM hello_blog_posts
  134. WHERE DATE_FORMAT( create_time,"%Y-%m-01 00:00:00")= DATE_FORMAT(#{archiveDate}, "%Y-%m-01 00:00:00" )
  135. </select>
  136. <select id="selectHotPostsList" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsVO">
  137. </select>
  138. <select id="selectByStatistics" resultType="com.sumbytes.helloblog.posts.domain.vo.PostsStatisticsVO">
  139. SELECT COUNT(1) as articleTotal,SUM(views) as viewsTotal,SUM(comments) as commentsTotal
  140. FROM `hello_blog_posts`
  141. </select>
  142. <update id="incrementView">
  143. UPDATE hello_blog_posts SET views = views + 1 WHERE id = #{id}
  144. </update>
  145. <update id="incrementComments">
  146. UPDATE hello_blog_posts SET comments = comments + 1 WHERE id = #{id}
  147. </update>
  148. </mapper>