<?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="wbl_ssm_blog.mapper.ArticleMapper"> <resultMap id="BaseResultMap" type="wbl_ssm_blog.entity.Article"> <id column="article_id" property="articleId" jdbcType="INTEGER"/> <result column="article_user_id" property="articleUserId" jdbcType="INTEGER"/> <result column="article_title" property="articleTitle" jdbcType="VARCHAR"/> <result column="article_content" property="articleContent" jdbcType="LONGVARCHAR"/> <result column="article_summary" property="articleSummary" jdbcType="VARCHAR"/> <result column="article_view_count" property="articleViewCount" jdbcType="INTEGER"/> <result column="article_comment_count" property="articleCommentCount" jdbcType="INTEGER"/> <result column="article_like_count" property="articleLikeCount" jdbcType="INTEGER"/> <result column="article_is_comment" property="articleIsComment" jdbcType="INTEGER"/> <result column="article_order" property="articleOrder" jdbcType="INTEGER"/> <result column="article_create_time" property="articleCreateTime" jdbcType="TIMESTAMP"/> <result column="article_update_time" property="articleUpdateTime" jdbcType="TIMESTAMP"/> <result column="article_status" property="articleStatus" jdbcType="INTEGER"/> </resultMap> <sql id="tb">article</sql> <sql id="Base_Column_List"> article_id, article_user_id, article_title, article_content,article_summary, article_view_count, article_comment_count, article_like_count, article_create_time, article_update_time, article_is_comment, article_status, article_order </sql> <!-- 测试 @Param语法 这种情况下不需要设置parameterType这个参数--> <!-- 数据库字段使用驼峰命名article_user_id 实体的使用userName命名 mybatis-config.xml中配置了驼峰匹配原则 --> <select id="findArticleBySome" resultType="Article"> select * from <include refid="tb"/> where article_title like '%${articleTitle}%' and article_view_count > #{articleViewCount} </select> <!--测试使用对象的形式为查询语句赋值--> <select id="findArticleBySome01" parameterType="Article" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_title like '%${articleTitle}%' and article_view_count > #{articleViewCount} </select> <!-- mybatis删除数据时 会返回被删除的行数 --> <delete id="deleteById" parameterType="Integer"> delete from <include refid="tb"/> where article_id = #{articleId} </delete> <!--这里要注意两个问题 第一个是获取插入的数据的主键 第二个是要在数据后面添加jdbcType 这样可以在插入空数据的时候自动转为空字符串--> <insert id="insert" parameterType="Article" useGeneratedKeys="true" keyProperty="articleId"> insert into <include refid="tb"/> (article_user_id, article_title, article_content,article_summary, article_view_count, article_comment_count, article_like_count, article_create_time,article_update_time, article_is_comment, article_status, article_order) values ( #{articleUserId,jdbcType=INTEGER },#{articleTitle,jdbcType=VARCHAR},#{articleContent,jdbcType=VARCHAR}, #{articleSummary,jdbcType=VARCHAR},#{articleViewCount,jdbcType=INTEGER},#{articleCommentCount,jdbcType=INTEGER}, #{articleLikeCount,jdbcType=INTEGER },#{articleCreateTime,jdbcType=TIMESTAMP },#{articleUpdateTime,jdbcType=TIMESTAMP}, #{articleIsComment,jdbcType=LONGVARCHAR},#{articleStatus,jdbcType=INTEGER },#{articleOrder,jdbcType=INTEGER } ) </insert> <!--<if>标签前面要加<set>标签 更新的字段后面要加jdbcType--> <update id="update" parameterType="Article"> update <include refid="tb"/> <set> <!--if的test条件里面取对象的值 不用带#{} 直接拿对象的属性值就可以--> <if test="articleUserId != null and articleUserId != '' ">article_user_id = #{articleUserId,jdbcType=INTEGER },</if> <if test="articleTitle != null and articleTitle != '' ">article_title = #{articleTitle,jdbcType=VARCHAR},</if> <if test="articleContent != null and articleContent != '' ">article_content = #{articleContent,jdbcType=VARCHAR},</if> <if test="articleSummary != null and articleSummary != '' ">article_summary = #{articleSummary,jdbcType=VARCHAR},</if> <if test="articleViewCount != null and articleViewCount != '' ">article_view_count = #{articleViewCount,jdbcType=INTEGER},</if> <if test="articleCommentCount != null and articleCommentCount != '' ">article_comment_count = #{articleCommentCount,jdbcType=INTEGER},</if> <if test="articleLikeCount != null and articleLikeCount != '' ">article_like_count = #{articleLikeCount,jdbcType=INTEGER },</if> <if test="articleCreateTime != null and articleCreateTime != '' ">article_create_time = #{articleCreateTime,jdbcType=TIMESTAMP },</if> <if test="articleUpdateTime != null and articleUpdateTime != '' ">article_update_time = #{articleUpdateTime,jdbcType=TIMESTAMP},</if> <if test="articleIsComment != null and articleIsComment != '' ">article_is_comment = #{articleIsComment,jdbcType=LONGVARCHAR},</if> <if test="articleStatus != null and articleStatus != '' ">article_status = #{articleStatus,jdbcType=INTEGER },</if> <if test="articleOrder != null and articleOrder != '' ">article_order = #{articleOrder,jdbcType=INTEGER },</if> </set> where article_id = #{articleId,jdbcType = INTEGER} </update> <!--这里根据查询条件对文章进行查询--> <select id="findAll" resultType="Article"> select article.* from <include refid="tb"/> <where> <!--取HashMap里面的东西时 直接取key就行--> <if test="status != null">article.article_status = #{status} AND </if> <!--mybatis做模糊查询时 使用concat方法拼接%keywords%--> <if test="keywords != null">article.article_title like concat(concat('%',#{keywords}),'%') AND </if> <!--这里忘了加AND差点被恶心死--> <if test="userId != null">article.article_user_id = #{userId} AND </if> <!--分类--> <if test="categoryId != null"> article.article_id in ( select article_category_ref.article_id from article_category_ref where article_category_ref.category_id = #{category_id} ) AND </if> <!--标签--> <if test="tagId != null"> article.article_id in ( select article_tag_ref.article_id from article_tag_ref where article_tag_ref.tag_id = #{tagId} ) AND </if> </where> 1 = 1 order by article.article_order DESC ,article.article_id DESC </select> <!--根据作者的id对文章进行归档 只查询部分属性--> <select id="listAllNotWithContent" resultType="Article"> select article_id,article_user_id,article_title,article_create_time from <include refid="tb"/> where article_status = 1 order by article_id desc </select> <!--根据id批量删除文章 这里的输入参数是一个list 并没有指定输入参数的类型--> <delete id="deleteBatch" > delete from <include refid="tb"/> <if test="ids != null"> where article_id in <!--使用foreach标签拼接sql语句--> <foreach collection="ids" open="(" separator="," close=")" item="id"> #{id} </foreach> </if> </delete> <!--统计所有已经启用点文章数量--> <select id="countArticle" parameterType="Integer" resultType="Integer"> select count(*) from <include refid="tb"/> where article_status = #{status} </select> <!--统计所有文章总的评论数 sum和后面的(之间不要留空格--> <select id="countArticleComment" resultType="Integer"> select sum(article_comment_count) from <include refid="tb"/> where article_status = 1 </select> <!--统计总的访问量--> <select id="countArticleView" resultType="Integer"> select sum(article_view_count) from <include refid="tb"/> where article_status = 1 </select> <!--根据文章的status和id获取文章--> <select id="getArticleByStatusAndId" resultType="Article"> select * from <include refid="tb"/> <where> <if test="id != null">article_id = #{id} AND</if> <if test="status != null">article_status = #{status}</if> </where> </select> <!--对文章进行分页操作 获取某一页文章的数据--> <select id="pageArtilce" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where <if test="status != null">article_status = #{status}</if> order by article_status ASC,article_id DESC,article_user_id DESC limit #{pageIndex},#{pageSize} </select> <!-- 统计limit条较受欢迎的文章--> <select id="listArticleByViewCount" parameterType="Integer" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_atatus = 1 ORDER BY article_view_count DESC ,article_order DESC ,article_id DESC limit #{limit} </select> <!-- 获取下一篇文章--> <select id="getAfterArticle" parameterType="Integer" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_status = 1 and article_id > #{id} order by article_id ASC limit 1 </select> <!--获取上一篇文章 这里居然不能写< 要写<--> <!-- Mybatis中的sql语句中的“<”浩和“>”号要用转义字符“<”和”>“,否则会报错! --> <select id="getPreArticle" parameterType="Integer" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_id < #{id} and article_status = 1 order by article_id DESC limit 1 </select> <!--获取随机文章--> <select id="listRadomArticle" parameterType="Integer" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_status = 1 order by rand() limit #{limit} </select> <!--根据文章的评论数获取文章--> <select id="listArticleByCommentCount" parameterType="Integer" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_status = 1 order by article_comment_count DESC ,article_id DESC ,article_user_id DESC limit #{limit} </select> <!--更新文章的评论数 mysql中sum count这种类型的函数和后面的括号之间不要留空格--> <update id="updateCommentCount" parameterType="Integer" > UPDATE <include refid="tb"/> set article_comment_count = ( select count(*) from comment where article.article_id = comment.comment_article_id ) where article_id = #{id} </update> <!--获得最后更新的记录--> <select id="getLastUpdateArticle" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_status = 1 order by article_update_time DESC limit 1 </select> <!--统计某个用户的文章数量--> <select id="countArticleByUser" parameterType="Integer" resultType="Integer"> select count(*) from <include refid="tb"/> where article_status = 1 and article_user_id = #{id} </select> <!--获取某一个分类的文章--> <select id="findArticleByCategoryId" resultType="Article"> select * from article,article_category_ref where article_category_ref.article_id = article.article_id and article_category_ref.category_id = #{id} and article_status = 1 limit #{limit} </select> <!--获取多个分类的文章--> <select id="findArticleByCategoryIds" resultType="Article"> select * from article,article_category_ref <where> article_category_ref.article_id = article.article_id and article_category_ref.category_id in <foreach collection="ids" item="id" open="(" separator="," close=")" > #{id} </foreach> and article.article_status = 1 </where> limit #{limit} </select> <!--根据文章创建时间获取最新创建的文章--> <select id="listArticleByLimit" parameterType="Integer" resultType="Article"> select <include refid="Base_Column_List"/> from <include refid="tb"/> where article_status = 1 order by article_create_time desc limit #{limit} </select> </mapper>
对应的java类
package wbl_ssm_blog.mapper; import org.apache.ibatis.annotations.Param; import wbl_ssm_blog.entity.Article; import java.util.HashMap; import java.util.List; public interface ArticleMapper { /** * 我自己编写的一个类 用来测试mybatis语句的写法 * @param articleTitle 当查询需要多个参数的时候 就使用@Param语法 或者传入一个对象进去1 * @return */ List<Article> findArticleBySome(@Param("articleTitle") String articleTitle,@Param("articleViewCount") Integer articleViewCount); /** * 需要传入多个参数时 新建一个查询对象 把查询参数赋值给这个查询对象 然后在sql语句中直接获取查询参数的值 * @param article * @return */ List<Article> findArticleBySome01(Article article); /** * 根据articleId删除文章 * @param articleId * @return 影响函数 估计是被删除的记录条数 */ Integer deleteById(@Param(value = "articleId") Integer articleId); /** * 添加文章 * @param article * @return */ Integer insert(Article article); /** * 更新文章 * @param article * @return */ Integer update(Article article); /** * 根据查询条件查找文章列表 * @param criteria * @return */ List<Article> findAll(HashMap<String,Object> criteria); /** * 文章归档 * @return */ List<Article> listAllNotWithContent(); /** * 统计文章数量 * @param status * @return */ Integer countArticle(@Param(value = "status") Integer status); /** * 获取留言数量 * @return */ Integer countArticleComment(); /** * 获取总的访问量 * @return */ Integer countArticleView(); /** * 获取所有文章 * @return */ List<Article> listArticle(); /** * 根据文章的status和id获取文章 * @param status * @param id * @return */ Article getArticleByStatusAndId(@Param("status") Integer status,@Param("id") Integer id); /** * 对文章进行分页操作 * @param status * @param pageIndex * @param pageSize * @return */ List<Article> pageArtilce(@Param(value = "status") Integer status,@Param(value = "pageIndex") Integer pageIndex,@Param(value = "pageSize") Integer pageSize); /** * 统计最受欢迎的文章 根据浏览量 * @param limit * @return */ List<Article> listArticleByViewCount(@Param(value="limit") Integer limit); /** * 获取下一篇文章 * @param id * @return */ Article getAfterArticle(@Param(value = "id") Integer id); /** * 获取上一篇文章 * @param id * @return */ Article getPreArticle(@Param(value="id") Integer id); /** * 获取随机文章 * @param limit * @return */ List<Article> listRadomArticle(@Param(value = "limit") Integer limit); /** * 根据文章评论数获取文章列表 * @param limit * @return */ List<Article> listArticleByCommentCount(@Param(value = "limit") Integer limit); /** * 更新文章的评论数 * @param articleId */ void updateCommentCount(@Param(value = "id") Integer articleId); /** * 获得最后更新的记录 * @return */ Article getLastUpdateArticle(); /** * 统计某个用户的文章数量 * @param id * @return */ Integer countArticleByUser(@Param(value = "id") Integer id); /** * 根据文章分类获取文章 * @param categoryId * @return */ List<Article> findArticleByCategoryId(@Param(value = "id") Integer categoryId,@Param(value = "limit") Integer limit); /** * 根据多个集合获取文章 * @param categoryIds * @return */ List<Article> findArticleByCategoryIds(@Param(value = "ids") List<Integer> categoryIds,@Param(value = "limit") Integer limit); /** * 获得最新文章 * * @param limit 查询数量 * @return 列表 */ List<Article> listArticleByLimit(Integer limit); /** * 批量删除文章 * * @param ids 文章Id列表 * @return 影响行数 */ Integer deleteBatch(@Param("ids") List<Integer> ids); }