4.Dao及MyBatis映射文件-查看文章

4.Dao及MyBatis映射文件

发表于:2017-06-24 10:02:04 分类:博客源码 阅读:1047次

image

1.BlogMapper

package top.ersredma.blog.dao;
import org.apache.ibatis.annotations.Param;
import top.ersredma.blog.bean.Blog;


public interface BlogMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Blog record);

    int insertSelective(Blog record);

    Blog selectByPrimaryKey(Integer id);

    Blog selectByUserId(@Param("user_id")Integer userid);

    int updateByPrimaryKeySelective(Blog record);

    int updateByPrimaryKey(Blog record);

    Blog selectByUrl(@Param("url")String url);

    void addBlogPV(Integer id);
}

2.BlogMapper.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.BlogMapper" >
  <resultMap id="BaseResultMap" type="top.ersredma.blog.bean.Blog" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="url" property="url" jdbcType="VARCHAR" />
    <result column="blogname" property="blogname" jdbcType="VARCHAR" />
    <result column="blogintro" property="blogintro" jdbcType="VARCHAR" />
    <result column="state" property="state" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="blogimg" property="blogimg" jdbcType="VARCHAR" />
    <result column="pv" property="pv" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, url, blogname, blogintro, state, user_id, blogimg, pv
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  select
  <include refid="Base_Column_List" />
  from blog
  where id = #{id,jdbcType=INTEGER}
</select>
  <select id="selectByUserId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from blog
    where user_id = #{user_id,jdbcType=INTEGER}
  </select>
  <select id="selectByUrl" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select
    <include refid="Base_Column_List" />
    from blog
    where url = #{url,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from blog
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="top.ersredma.blog.bean.Blog" >
    insert into blog (id, url, blogname, 
      blogintro, state, user_id, 
      blogimg, pv)
    values (#{id,jdbcType=INTEGER}, #{url,jdbcType=VARCHAR}, #{blogname,jdbcType=VARCHAR}, 
      #{blogintro,jdbcType=VARCHAR}, #{state,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, 
      #{blogimg,jdbcType=VARCHAR}, #{pv,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="top.ersredma.blog.bean.Blog" >
    insert into blog
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="url != null" >
        url,
      </if>
      <if test="blogname != null" >
        blogname,
      </if>
      <if test="blogintro != null" >
        blogintro,
      </if>
      <if test="state != null" >
        state,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="blogimg != null" >
        blogimg,
      </if>
      <if test="pv != null" >
        pv,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="url != null" >
        #{url,jdbcType=VARCHAR},
      </if>
      <if test="blogname != null" >
        #{blogname,jdbcType=VARCHAR},
      </if>
      <if test="blogintro != null" >
        #{blogintro,jdbcType=VARCHAR},
      </if>
      <if test="state != null" >
        #{state,jdbcType=INTEGER},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="blogimg != null" >
        #{blogimg,jdbcType=VARCHAR},
      </if>
      <if test="pv != null" >
        #{pv,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="addBlogPV" >
    UPDATE  blog set pv=pv+1 where id=#{id}
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="top.ersredma.blog.bean.Blog" >
    update blog
    <set >
      <if test="url != null" >
        url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="blogname != null" >
        blogname = #{blogname,jdbcType=VARCHAR},
      </if>
      <if test="blogintro != null" >
        blogintro = #{blogintro,jdbcType=VARCHAR},
      </if>
      <if test="state != null" >
        state = #{state,jdbcType=INTEGER},
      </if>
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="blogimg != null" >
        blogimg = #{blogimg,jdbcType=VARCHAR},
      </if>
      <if test="pv != null" >
        pv = #{pv,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="top.ersredma.blog.bean.Blog" >
    update blog
    set url = #{url,jdbcType=VARCHAR},
      blogname = #{blogname,jdbcType=VARCHAR},
      blogintro = #{blogintro,jdbcType=VARCHAR},
      state = #{state,jdbcType=INTEGER},
      user_id = #{userId,jdbcType=INTEGER},
      blogimg = #{blogimg,jdbcType=VARCHAR},
      pv = #{pv,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

3.BlogRollMapper

package top.ersredma.blog.dao;

import top.ersredma.blog.bean.BlogRoll;

import javax.annotation.Resource;

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

    int insert(BlogRoll record);

    int insertSelective(BlogRoll record);

    BlogRoll selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(BlogRoll record);

    int updateByPrimaryKey(BlogRoll record);
}

4.BlogRollMapper.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.BlogRollMapper" >
  <resultMap id="BaseResultMap" type="top.ersredma.blog.bean.BlogRoll" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="describe" property="describe" jdbcType="VARCHAR" />
    <result column="url" property="url" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    r.id, r.user_id, r.describe, r.url
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from blogroll r
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from blogroll
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="top.ersredma.blog.bean.BlogRoll" >
    insert into blogroll
    values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{describe,jdbcType=VARCHAR}, 
      #{url,jdbcType=VARCHAR})
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="top.ersredma.blog.bean.BlogRoll" >
    update blogroll r
    <set >
      <if test="userId != null" >
        r.user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="describe != null" >
        r.describe = #{describe,jdbcType=VARCHAR},
      </if>
      <if test="url != null" >
        r.url = #{url,jdbcType=VARCHAR},
      </if>
    </set>
    where r.id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="top.ersredma.blog.bean.BlogRoll" >
    update blogroll r
    set r.user_id = #{userId,jdbcType=INTEGER},
      r.describe = #{describe,jdbcType=VARCHAR},
      r.url = #{url,jdbcType=VARCHAR}
    where r.id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

待续。。。

关键词:blog源码,dao


验证码: