BizArticleMapper.xml 7.5 KB
Newer Older
Y
yadong.zhang 已提交
1 2 3 4 5 6 7 8 9 10
<?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.zyd.blog.persistence.mapper.BizArticleMapper">
	<resultMap id="rm" type="com.zyd.blog.persistence.beans.BizArticle">
		<result property="id" jdbcType="BIGINT" column="id"/>
		<result property="title" jdbcType="VARCHAR" column="title"/>
		<result property="userId" jdbcType="BIGINT" column="user_id"/>
		<result property="coverImage" jdbcType="VARCHAR" column="cover_image"/>
		<result property="qrcodePath" jdbcType="VARCHAR" column="qrcode_path"/>
Y
yadong.zhang 已提交
11
		<result property="isMarkdown" jdbcType="BIT" column="is_markdown"/>
Y
yadong.zhang 已提交
12
		<result property="content" jdbcType="VARCHAR" column="content"/>
Y
yadong.zhang 已提交
13
		<result property="contentMd" jdbcType="VARCHAR" column="content_md"/>
Y
yadong.zhang 已提交
14 15 16 17 18 19
		<result property="typeId" jdbcType="BIGINT" column="type_id"/>
		<result property="status" jdbcType="INTEGER" column="status"/>
		<result property="top" jdbcType="BIT" column="top"/>
		<result property="recommended" jdbcType="BIT" column="recommended"/>
		<result property="original" jdbcType="BIT" column="original"/>
		<result property="description" jdbcType="VARCHAR" column="description"/>
20
		<result property="editorType" jdbcType="VARCHAR" column="editor_type"/>
Y
yadong.zhang 已提交
21 22 23
		<result property="keywords" jdbcType="VARCHAR" column="keywords"/>
		<result property="createTime" jdbcType="TIMESTAMP" column="create_time"/>
		<result property="updateTime" jdbcType="TIMESTAMP" column="update_time"/>
Y
yadong.zhang 已提交
24
		<result property="comment" jdbcType="BIT" column="comment"/>
25
		<result property="password" jdbcType="VARCHAR" column="password"/>
26
		<result property="requiredAuth" jdbcType="BIT" column="required_auth"/>
Y
yadong.zhang 已提交
27 28 29 30 31 32 33

		<result property="lookCount" jdbcType="INTEGER" column="look_count"/>
		<result property="commentCount" jdbcType="INTEGER" column="comment_count"/>
		<result property="loveCount" jdbcType="INTEGER" column="love_count"/>
		<association property="bizType" javaType="com.zyd.blog.persistence.beans.BizType">
			<result property="id" jdbcType="BIGINT" column="btype_id"/>
			<result property="name" jdbcType="VARCHAR" column="btype_name"/>
34
			<result property="icon" jdbcType="VARCHAR" column="btype_icon"/>
Y
yadong.zhang 已提交
35 36 37 38 39 40 41 42 43 44 45
			<result property="description" jdbcType="VARCHAR" column="btype_description"/>
		</association>
		<collection property="tags" column="tag_id" javaType="ArrayList" ofType="com.zyd.blog.persistence.beans.BizTags">
			<result property="id" jdbcType="BIGINT" column="tag_id"/>
			<result property="name" jdbcType="VARCHAR" column="tag_name"/>
			<result property="description" jdbcType="VARCHAR" column="tag_description"/>
		</collection>
	</resultMap>

	<select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.ArticleConditionVO" resultMap="rm">
		SELECT
智布道's avatar
智布道 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
			a.id,
			a.title,
			a.user_id,
			a.cover_image,
			a.qrcode_path,
			a.is_markdown,
			a.top,
			a.type_id,
			a.`status`,
			a.recommended,
			a.original,
			a.description,
			a.keywords,
			a.`comment`,
60
			a.`editor_type`,
61
			a.`password`,
62
			a.`required_auth`,
智布道's avatar
智布道 已提交
63 64
			a.create_time,
			a.update_time,
Y
yadong.zhang 已提交
65 66
			btype.id AS btype_id,
			btype.`name` AS btype_name,
67
			btype.`icon` AS btype_icon,
68
			btype.description AS btype_description
Y
yadong.zhang 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		FROM
            biz_article a
		INNER JOIN biz_type btype ON a.type_id = btype.id
		INNER JOIN biz_article_tags atag ON a.id = atag.article_id
		WHERE 1 = 1
        <if test="typeId != null">
            AND	a.type_id = #{typeId}
        </if>
		<choose>
			<when test="tagIds != null">
				AND atag.tag_id IN (
					<foreach collection="tagIds" item="tagId" separator=",">
						#{tagId}
					</foreach>
				)
			</when>
			<otherwise>
				<if test="tagId != null">
					AND	atag.tag_id = #{tagId}
				</if>
			</otherwise>
		</choose>
		<if test="top != null">
			AND	a.top = #{top}
		</if>
		<if test="status != null">
			AND	a.status = #{status}
		</if>
		<if test="recommended != null">
			AND	a.recommended = #{recommended}
		</if>
		<if test="keywords != null and keywords != '' ">
			AND
			(
				a.description LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%') OR
				a.keywords LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%')
			)
		</if>
107 108 109 110 111
		<if test="title != null and title != '' ">
			AND (
			a.title LIKE CONCAT('%',#{title , jdbcType=VARCHAR},'%')
			)
		</if>
Y
yadong.zhang 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        GROUP BY a.id
		<choose>
			<when test="random != null">
				ORDER BY RAND()
			</when>
			<otherwise>
				ORDER BY
				a.top DESC,
				a.recommended DESC,
				a.create_time DESC
			</otherwise>
		</choose>
	</select>

	<select id="listTagsByArticleId" parameterType="List" resultMap="rm">
		SELECT
128
            a.id,
Y
yadong.zhang 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            t.id AS tag_id,
            t.`name` AS tag_name,
            t.description AS tag_description
        FROM
            biz_article a
        INNER JOIN biz_article_tags atag ON a.id = atag.article_id
        INNER JOIN biz_tags t ON atag.tag_id = t.id
        WHERE
            a.id IN (
                <foreach collection="list" item="id" separator=",">
                    #{id}
                </foreach>
            )
	</select>

	<select id="get" parameterType="Long" resultMap="rm">
		SELECT
智布道's avatar
智布道 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
			a.id,
			a.title,
			a.user_id,
			a.cover_image,
			a.qrcode_path,
			a.is_markdown,
			a.content,
			a.content_md,
			a.top,
			a.type_id,
			a.`status`,
			a.recommended,
			a.original,
159
			a.editor_type,
智布道's avatar
智布道 已提交
160 161 162
			a.description,
			a.keywords,
			a.`comment`,
163
			a.`password`,
164
			a.`required_auth`,
智布道's avatar
智布道 已提交
165 166
			a.create_time,
			a.update_time,
Y
yadong.zhang 已提交
167 168 169 170 171
			btype.id AS btype_id,
			btype.`name` AS btype_name,
			btype.description AS btype_description,
			t.id AS tag_id,
			t.`name` AS tag_name,
172
			t.description AS tag_description
Y
yadong.zhang 已提交
173 174 175 176
		FROM
			biz_article a
		INNER JOIN biz_type btype ON a.type_id = btype.id
		INNER JOIN biz_article_tags atag ON a.id = atag.article_id
智布道's avatar
智布道 已提交
177
		LEFT JOIN biz_tags t ON atag.tag_id = t.id
Y
yadong.zhang 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		WHERE
			a.id = #{id}
	</select>

	<!-- 获取上一篇和下一篇文章 -->
	<select id="getPrevAndNextArticles"  parameterType="Date" resultMap="rm">
		(
			SELECT
				a.id,
				a.title,
				a.create_time
			FROM
				biz_article a
			WHERE
				a.create_time &lt; #{insertTime} AND  a.status = 1
			ORDER BY
				a.create_time DESC
			LIMIT 1
		)
		UNION ALL
		(
			SELECT
				a.id,
				a.title,
				a.create_time
			FROM
				biz_article a
			WHERE
				a.create_time &gt; #{insertTime} AND  a.status = 1
			ORDER BY
				a.create_time
			LIMIT 1
		)
	</select>

	<!-- 获取热门文章 -->
	<select id="listHotArticle" resultMap="rm">
		SELECT
智布道's avatar
智布道 已提交
216 217 218 219 220 221 222 223 224 225 226
			a.id,
			a.title,
			a.user_id,
			a.cover_image,
			a.qrcode_path,
			a.is_markdown,
			a.top,
			a.type_id,
			a.`status`,
			a.recommended,
			a.original,
227
			a.editor_type,
智布道's avatar
智布道 已提交
228 229 230 231 232
			a.description,
			a.keywords,
			a.`comment`,
			a.create_time,
			a.update_time,
233 234
			l.lookCount AS look_count,
			c.commentCount AS comment_count
Y
yadong.zhang 已提交
235 236 237 238 239 240 241 242 243 244 245
		FROM
			biz_article a
		LEFT JOIN (
			SELECT
				l.article_id,
				IFNULL(count(1), 0) AS lookCount
			FROM
				biz_article_look l
			GROUP BY
				l.article_id
		) l ON a.id = l.article_id
246 247 248 249 250 251 252 253 254 255
		LEFT JOIN (
			SELECT
				c.sid,
				IFNULL(count(1), 0) AS commentCount
			FROM
				biz_comment c
			WHERE c.`status` = 'APPROVED'
			GROUP BY
				c.sid
		) c ON a.id = c.sid
Y
yadong.zhang 已提交
256 257 258 259
		WHERE a.status = 1
		ORDER BY
			l.lookCount DESC
	</select>
260

Y
yadong.zhang 已提交
261 262 263 264 265 266 267 268 269
	<select id="isExist" parameterType="Long" resultType="Integer">
		SELECT
			COUNT(a.id)
		FROM
			biz_article a
		WHERE
			a.id = #{id}
	</select>

270 271 272 273 274 275 276 277 278 279
	<update id="batchUpdateStatus" parameterType="map">
		UPDATE `biz_article`
		SET status = #{status}
		WHERE
		 	id IN (
		<foreach collection="list" item="item" separator=",">
			#{item}
		</foreach>
		)
	</update>
Y
yadong.zhang 已提交
280 281
</mapper>