6.再续Dao及MyBatis的映射配置-查看文章

6.再续Dao及MyBatis的映射配置

发表于:2017-06-24 10:09:23 分类:博客源码 阅读:1223次

11.EssayMapper

package top.ersredma.blog.dao;


import org.apache.ibatis.annotations.Param;
import top.ersredma.blog.bean.Essay;
import top.ersredma.blog.bean.KeyWord;

import javax.annotation.Resource;
import java.util.List;

@Resource
public interface EssayMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Essay record);

    int insertSelective(Essay record);

    Essay selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Essay record);

    int updateByPrimaryKeyWithBLOBs(Essay record);

    int updateByPrimaryKey(Essay record);

    long selectCountByCase(@Param("userid")Integer userid, @Param("cate")int cate);

    List<Essay> selectByCase(@Param("userid")Integer userid, @Param("cate")int cate, @Param("model")String model, @Param("pageStart")int pageStart,@Param("pageSize") int pageSize);

    List<Essay> selectByKeyword(@Param("key")String key);

    void addEssayPV(@Param("id")Integer id);

    List<Essay> selectHotList();

    List<Essay> selectNewList();

    List<Essay> selectLikeList();

    List selectEssaysByKey(String key);
}

12.EssayMapper.xml

<?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="top.ersredma.blog.dao.EssayMapper" >
  <resultMap id="BaseResultMap" type="top.ersredma.blog.bean.Essay" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="time" property="time" jdbcType="VARCHAR" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="keyword" property="keyword" jdbcType="VARCHAR" />
    <result column="state" property="state" jdbcType="INTEGER" />
    <result column="category_id" property="categoryId" jdbcType="INTEGER" />
    <result column="pviews" property="pviews" jdbcType="INTEGER" />
    <result column="likenum" property="likenum" jdbcType="INTEGER" />
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="top.ersredma.blog.bean.Essay" extends="BaseResultMap" >
    <result column="content" property="content" jdbcType="LONGVARCHAR" />
  </resultMap>
  <!--<resultMap id="counts" type="java.lang.Long">-->
    <!--<result column="count" property="content"/>-->
  <!--</resultMap>-->
  <sql id="Base_Column_List" >
    id, title, time, user_id, keyword, state, category_id, pviews, likenum,imgurl
  </sql>
  <sql id="Blob_Column_List" >
    content
  </sql>
  <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from essay
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="selectByKeyword" resultMap="ResultMapWithBLOBs">
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from essay
    where keyword like concat(concat('%', #{key,jdbcType=VARCHAR}),'%') ORDER BY pviews desc limit 10
  </select>

  <select id="selectEssaysByKey" resultMap="ResultMapWithBLOBs">
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from essay
    where keyword like concat(concat('%', #{key,jdbcType=VARCHAR}),'%') or title like concat(concat('%', #{key,jdbcType=VARCHAR}),'%')
  </select>
  <select id="selectByCase" resultMap="ResultMapWithBLOBs">
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from essay
    where
    <if test="cate != -1" >
      category_id = #{cate,jdbcType=INTEGER} and
    </if>
    user_id = #{userid,jdbcType=INTEGER} and state=0 order by id ${model} limit #{pageStart,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
  </select>

  <select id="selectHotList" resultMap="ResultMapWithBLOBs">
    SELECT
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    FROM essay e,
    (SELECT c.essay_id essayid ,COUNT(*) counts FROM COMMENT c WHERE c.essay_id IN
    (SELECT essay_id FROM COMMENT)
    GROUP BY c.essay_id
    ORDER BY counts DESC limit 10)temp
    WHERE e.id=temp.essayid
  </select>

  <select id="selectNewList" resultMap="ResultMapWithBLOBs">
    SELECT
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    FROM essay ORDER BY id DESC LIMIT 10
  </select>

  <select id="selectLikeList" resultMap="ResultMapWithBLOBs">
    SELECT
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    FROM essay ORDER BY pviews DESC LIMIT 10
  </select>

  <select id="selectCountByCase" resultType="java.lang.Long" >
    select
    COUNT(e.id)
    from essay e
    where
    <if test="cate != -1" >
      category_id = #{cate,jdbcType=INTEGER} and
    </if>
    user_id = #{userid,jdbcType=INTEGER}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from essay
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="top.ersredma.blog.bean.Essay" >
    insert into essay (id, title, time, 
      user_id, keyword, state, 
      category_id, pviews, likenum, 
      content,imgurl)
    values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{time,jdbcType=VARCHAR}, 
      #{userId,jdbcType=INTEGER}, #{keyword,jdbcType=VARCHAR}, #{state,jdbcType=INTEGER}, 
      #{categoryId,jdbcType=INTEGER}, #{pviews,jdbcType=INTEGER}, #{likenum,jdbcType=INTEGER}, 
      #{content,jdbcType=LONGVARCHAR},#{imgurl,jdbcType=VARCHAR},)
  </insert>
  <insert id="insertSelective" parameterType="top.ersredma.blog.bean.Essay" useGeneratedKeys="true" keyProperty="id">
    insert into essay
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="time != null" >
        time,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="keyword != null" >
        keyword,
      </if>
      <if test="state != null" >
        state,
      </if>
      <if test="categoryId != null" >
        category_id,
      </if>
      <if test="pviews != null" >
        pviews,
      </if>
      <if test="likenum != null" >
        likenum,
      </if>
      <if test="content != null" >
        content,
      </if>
      <if test="imgurl != null" >
        imgurl,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="title != null" >
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="time != null" >
        #{time,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="keyword != null" >
        #{keyword,jdbcType=VARCHAR},
      </if>
      <if test="state != null" >
        #{state,jdbcType=INTEGER},
      </if>
      <if test="categoryId != null" >
        #{categoryId,jdbcType=INTEGER},
      </if>
      <if test="pviews != null" >
        #{pviews,jdbcType=INTEGER},
      </if>
      <if test="likenum != null" >
        #{likenum,jdbcType=INTEGER},
      </if>
      <if test="content != null" >
        #{content,jdbcType=LONGVARCHAR},
      </if>
      <if test="imgurl != null" >
        #{imgurl,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="addEssayPV">
    UPDATE essay set pviews=pviews+1 where id=#{id}
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="top.ersredma.blog.bean.Essay" >
    update essay
    <set >
      <if test="title != null" >
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="time != null" >
        time = #{time,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="keyword != null" >
        keyword = #{keyword,jdbcType=VARCHAR},
      </if>
      <if test="state != null" >
        state = #{state,jdbcType=INTEGER},
      </if>
      <if test="categoryId != null" >
        category_id = #{categoryId,jdbcType=INTEGER},
      </if>
      <if test="pviews != null" >
        pviews = #{pviews,jdbcType=INTEGER},
      </if>
      <if test="likenum != null" >
        likenum = #{likenum,jdbcType=INTEGER},
      </if>
      <if test="content != null" >
        content = #{content,jdbcType=LONGVARCHAR},
      </if>
      <if test="imgurl != null" >
       imgurl= #{imgurl,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateByPrimaryKeyWithBLOBs" parameterType="top.ersredma.blog.bean.Essay" >
    update essay
    set title = #{title,jdbcType=VARCHAR},
      time = #{time,jdbcType=VARCHAR},
      user_id = #{userId,jdbcType=INTEGER},
      keyword = #{keyword,jdbcType=VARCHAR},
      state = #{state,jdbcType=INTEGER},
      category_id = #{categoryId,jdbcType=INTEGER},
      pviews = #{pviews,jdbcType=INTEGER},
      likenum = #{likenum,jdbcType=INTEGER},
      content = #{content,jdbcType=LONGVARCHAR},
      imgurl = #{imgurl,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="top.ersredma.blog.bean.Essay" >
    update essay
    set title = #{title,jdbcType=VARCHAR},
      time = #{time,jdbcType=VARCHAR},
      user_id = #{userId,jdbcType=INTEGER},
      keyword = #{keyword,jdbcType=VARCHAR},
      state = #{state,jdbcType=INTEGER},
      category_id = #{categoryId,jdbcType=INTEGER},
      pviews = #{pviews,jdbcType=INTEGER},
      likenum = #{likenum,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

再续。。。

关键词:blog源码,dao


验证码:

  1. author
    樵子(伪装者) 2017-07-05 12:21:05
    回复:杨铁心

    asdfaf

    你好
  2. author
    杨铁心(伪装者) 2017-07-05 12:20:14
    asdfaf
  3. author
    吕文德(伪装者) 2017-06-27 21:21:58
    回复:汤祖德

    2222222222222222

    2222222
  4. author
    汤祖德(伪装者) 2017-06-27 21:21:50
    2222222222222222
  5. author
    黄蓉(伪装者) 2017-06-27 16:05:09
    this is what???
  6. author
    都史(伪装者) 2017-06-26 14:04:44
    ffff
  7. author
    裘千丈(伪装者) 2017-06-26 10:14:21
    <>
  8. author
    李萍(伪装者) 2017-06-25 18:07:42
    >''>
  9. author
    沙通天(伪装者) 2017-06-24 23:29:31
    测试评论!