EduCoder笔记--博客系统- 博客管理

博客系统 - 博客管理

第1关:个人博客列表查询

Step1Mapper.java

package net.educoder.mapper;

import net.educoder.entity.TBlog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface Step1Mapper {


    /**
     *
     * 通过用户ID,查询该用户的所有博客
     *
     * @param userId  用户ID
     * @return 返回博客列表
     */

    /********** Begin **********/
    @Select("select * from t_blog where userId = #{userId}")
    List<TBlog> findAllBlogByUserId(Integer userId);
    /********** End **********/
}

Step1ServiceImpl.java

package net.educoder.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.educoder.entity.TBlog;
import net.educoder.mapper.Step1Mapper;
import net.educoder.service.Step1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class Step1ServiceImpl implements Step1Service {

    /**
     * Step1Mapper 对象注入
     */
    @Autowired
    Step1Mapper mapper;


    /**
     * 用户博客列表分页
     *
     * @param pageNum  页数
     * @param pageSize 显示条数
     * @param userId   用户ID
     * @return
     */
    @Override
    public PageInfo pageQueryByUser(Integer pageNum, Integer pageSize, Integer userId) {
        /********** Begin **********/
        PageHelper.startPage(pageNum, pageSize);
        List<TBlog> list = mapper.findAllBlogByUserId(userId);
        PageInfo pageInfo = new PageInfo(list);
        return  pageInfo;
        /********** End **********/
    }
}

Step1Controller.java

package net.educoder.controller;

import com.github.pagehelper.PageInfo;
import net.educoder.service.Step1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/blog")
public class Step1Controller {


    /**
     * Step1Service 对象注入
     */
    @Autowired
    private Step1Service service;

    /**
     * 通过用户ID,查询博客列表
     *
     * @param pageNum  页数
     * @param pageSize 显示条数
     * @param userId   用户ID
     * @return
     */
    @RequestMapping("/pageQueryByUser")
    @ResponseBody
    public PageInfo pageQueryByUser(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Integer userId) {

        /********** Begin **********/
        PageInfo pageInfo = service.pageQueryByUser(pageNum, pageSize, userId);
        return pageInfo;
        /********** End **********/
    }
}

第2关:删除博客

Step2Mapper.java

package net.educoder.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface Step2Mapper {


    /**
     * 通过博客ID 删除博客
     *
     * @param blogId 博客ID
     */
    /********** Begin **********/
    @Delete("delete from t_blog where blogId = #{blogId}")
    void deleteBlog(String blogId);
    /********** End **********/
}

Step2ServiceImpl.java

package net.educoder.service.impl;

import net.educoder.mapper.Step2Mapper;
import net.educoder.service.Step2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class Step2ServiceImpl implements Step2Service {


    /**
     * Step2Mapper 对象注入
     */
    @Autowired
    Step2Mapper mapper;


    /**
     * 通过博客ID 删除博客
     *
     * @param blogId 博客ID
     * @return Map
     */
    @Override
    public Map<String, Object> deleteBlog(String blogId) {
        Map<String, Object> map = new HashMap<>();
        /********** Begin **********/
        mapper.deleteBlog(blogId);

        /********** End **********/
        map.put("code", 200);
        return map;
    }
}

Step2Controller.java

package net.educoder.controller;

import net.educoder.service.Step2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/blog")
public class Step2Controller {

    /**
     * Step2Service 对象注入
     */
    @Autowired
    Step2Service service;

    /**
     * 删除博客
     *
     * @param blogId 博客ID
     * @return JSON
     */
    @RequestMapping("/deleteBlog")
    @ResponseBody
    public Map<String, Object> deleteBlog(String blogId) {
        /********** Begin **********/
        Map<String, Object> map = service.deleteBlog(blogId);
        return map;
        /********** End **********/
    }

}

第3关:编辑博客

Step3Mapper.java

package net.educoder.mapper;

import net.educoder.entity.TBlog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface Step3Mapper {

	/********** Begin **********/
    /**
     * 通过博客ID ,查询博客信息
     *
     * @param blogId 博客ID
     * @return 博客对象
     *
     * <p>
     * 使用的表:t_blog
     */
    @Select("select * from t_blog where blogId = #{blogId}")
    TBlog findBlogDetail(String blogId);


    /**
     * 更新博客信息
     *
     * @param blog 博客对象
     *
     * <p>
     * 使用的表:t_blog
     */
    @Update("update t_blog set blogTitle = #{blogTitle}, blogContent = #{blogContent}, typeId = #{typeId}, updateTime = #{updateTime}, coverImage = #{coverImage} where blogId = #{blogId}")
    void updateBlog(TBlog blog);

    /**
     * 博客ID与标签ID进行绑定
     *
     * @param blogId 博客ID
     * @param tagId  标签ID
     *
     * <p>
     * 使用的表:t_tag_blog
     */
    @Update("update t_tag_blog set tagId = #{tagId} where blogId = #{blogId}")
    void updateBlogAndTag(@Param("blogId") long blogId, @Param("tagId") String tagId);
    
    /********** End **********/
}

Step3ServiceImpl.java

package net.educoder.service.impl;

import net.educoder.entity.TBlog;
import net.educoder.mapper.Step3Mapper;
import net.educoder.service.Step3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class Step3ServiceImpl implements Step3Service {

    /**
     * Step3Mapper 对象注入
     */
    @Autowired
    Step3Mapper mapper;

    /**
     * 通过博客ID,查询博客信息
     *
     * @param blogId 博客ID
     * @return 博客对象
     */
    @Override
    public TBlog findTblog(String blogId) {
        /********** Begin **********/
        TBlog blogDetail = mapper.findBlogDetail(blogId);
        return blogDetail;
        /********** End **********/
    }

    /**
     * 修改博客
     *
     * @param blog  博客对象
     * @param tagId 标签ID
     */
    @Override
    public void updateBlog(TBlog blog, String tagId) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format.format(date);

        /********** Begin **********/
        //1.设置更新时间
        blog.setUpdateTime(dateStr);
        //2.更新博客信息
        mapper.updateBlog(blog);
        //3.获取博客ID
        long blogId = blog.getBlogId();
        //4.博客ID 与 标签ID 进行绑定
        mapper.updateBlogAndTag(blogId, tagId);
        /********** End **********/
    }
}

Step3Controller.java

package net.educoder.controller;

import net.educoder.entity.TBlog;
import net.educoder.service.Step3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/blog")
public class Step3Controller {


    /**
     * Step3Service 对象注入
     */
    @Autowired
    Step3Service service;

    /**
     * 通过博客ID 查询博客信息
     *
     * @param blogId 博客ID
     * @return JSON 数据,格式如下:
     * <p>
     * {
     * "blog": 博客对象
     * }
     */
    @RequestMapping("/findTblog")
    @ResponseBody
    public Map<String, Object> findTblog(String blogId) {
        Map<String, Object> map = new HashMap<>();
        /********** Begin **********/
        TBlog tblog = service.findTblog(blogId);
        map.put("blog", tblog);
        /********** End **********/
        return map;
    }


    /**
     * 修改博客
     *
     * @param blog  博客对象
     * @param tagId 标签ID
     * @return JSON 数据,格式如下:
     * <p>
     * {
     * "code":200,
     * "blogId":1
     * }
     */
    @RequestMapping("/updateBlog")
    @ResponseBody
    public Map<String, Object> updateBlog(TBlog blog, String tagId) {
        Map<String, Object> map = new HashMap<>();
        /********** Begin **********/
        service.updateBlog(blog, tagId);
        long blogId = blog.getBlogId();
        map.put("code", 200);
        map.put("blogId", blogId);
        /********** End **********/
        return map;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值