EduCoder笔记--博客系统- 博客列表

博客系统 - 博客列表

第1关:博客列表查询 - 不进行分页

BlogMapper.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 BlogMapper {
	
    /********** Begin **********/

    /**
     * 博客列表(获取所有博客)
     *
     * @return  返回博客列表
     */
    @Select("select * from t_blog")
    List<TBlog> pageQuery();

    /********** End **********/
}

BlogServiceImpl.java

package net.educoder.service.impl;

import com.github.pagehelper.PageInfo;
import net.educoder.entity.TBlog;
import net.educoder.mapper.BlogMapper;
import net.educoder.service.BlogService;
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 BlogServiceImpl implements BlogService {

    /**
     * 注入 BlogMapper 对象
     */
    @Autowired
    private BlogMapper mapper;

    /**
     * 不分页获取博客列表
     *
     * @return 返回博客列表
     */
    @Override
    public List<TBlog> pageQuery() {
        /********** Begin **********/
        List<TBlog> list = mapper.pageQuery();
        return list;
        /********** End **********/
    }

}

Step1Controller.java

package net.educoder.controller;

import net.educoder.entity.TBlog;
import net.educoder.service.BlogService;
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.List;
import java.util.Map;

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


    @Autowired
    private BlogService service;

    /**
     * 不进行分页,返回博客列表
     *
     * @return JSON数据,数据格式如下:
     * <p>
     * {
     * "nextPage": 0,
     * "list": [{
     * "blogId": 1,
     * "blogTitle": "Hadoop大数据入门课程",
     * "blogContent": "#### 环境安装\r\n环境安装是使用一个开发工具的第一步\r\n\r\n\r\n![](https://www.educoder.net/attachments/download/186245)\r\n\r\n\r\n#### HDFS\r\n\r\n#### HBASE\r\n\r\n#### HIVE",
     * "userId": 3,
     * "typeId": 1,
     * "blogStatus": 1,
     * "createTime": "2018-04-28 08:55:55.0",
     * "updateTime": "2018-04-28 08:55:55.0",
     * "coverImage": "/img/a.jpg"
     * }, {
     * "blogId": 2,
     * "blogTitle": "大数据进阶课程",
     * "blogContent": "## MapReduce\r\n\r\n分为两个过程  \r\nMap过程\r\n\r\nReduce过程\r\n\r\n```java\r\npublic static void main(){\r\n\r\n\r\n}\r\n\r\n```",
     * "userId": 3,
     * "typeId": 2,
     * "blogStatus": 0,
     * "createTime": "2018-04-28 08:57:16.0",
     * "updateTime": "2018-04-28 08:57:16.0",
     * "coverImage": null
     * }]
     * }
     */
    @RequestMapping("/pageQuery")
    @ResponseBody
    public Map<String, Object> pageQuery() {
        Map<String, Object> map = new HashMap<>();
        /********** Begin **********/
       List<TBlog> list = service.pageQuery();
       map.put("list", list);
       map.put("nextPage", 0);
        /********** End **********/
        return map;
    }
}

第2关:博客列表查询 - 分页+分类

BlogMapper.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 BlogMapper {

    /********** Begin **********/

    /**
     * 根据博客类别,查询博客列表
     *
     * @param typeId 博客类别
     * @return 博客列表
     */
    @Select("select * from t_blog where typeId = #{typeId}")
    List<TBlog> findAllBlogByTypeId(Integer typeId);

    /**
     * 查询博客列表
     *
     * @return 博客列表
     */
    @Select("select * from t_blog")
    List<TBlog> findAllBlog();

    /********** End **********/
}

BlogServiceImpl.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.BlogMapper;
import net.educoder.service.BlogService;
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 BlogServiceImpl implements BlogService {


    /**
     * BlogMapper 对象注入
     */
    @Autowired
    private BlogMapper mapper;


    /**
     * 博客列表分页
     *
     * @param pageNum  页数
     * @param pageSize 显示条数
     * @param typeId   博客类型ID
     * @return 博客列表
     */
    @Override
    public PageInfo pageQuery(Integer pageNum, Integer pageSize, Integer typeId) {

        /********** Begin **********/

        //1.PageHelper 设置 startPage 的相关参数
        PageHelper.startPage(pageNum, pageSize);
        //2.判断 typeId 是否为 0 ,如果为 0 ,不进行博客类型筛选(调用 findAllBlog 方法);反之,进行博客类型筛选(调用 findAllBlogByTypeId 方法)
        List<TBlog> list = null;
        if(typeId == 0) {
            list = mapper.findAllBlog();
        } else {
            list = mapper.findAllBlogByTypeId(typeId);
        }

        //3,创建 PageInfo ,进行分页
        PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
        /********** End **********/
    }
}

Step2Controller.java

package net.educoder.controller;

import com.github.pagehelper.PageInfo;
import net.educoder.service.BlogService;
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 Step2Controller {


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

    /**
     * 博客列表分页
     *
     * @param pageNum  页数
     * @param pageSize 显示条数
     * @return 返回 PageInfo 对象
     */
    @RequestMapping("/pageQuery")
    @ResponseBody
    public PageInfo pageQuery(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                              @RequestParam(value = "typeId", defaultValue = "0") Integer typeId) {
        /********** Begin **********/
        PageInfo pageInfo =service.pageQuery(pageNum, pageSize, typeId);        
        return pageInfo;
		/********** End **********/
    }
}

PageHelperConfig.java

package net.educoder.conf;

import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class PageHelperConfig {


    @Bean
    public PageHelper getPageHelper() {
        Properties properties = new Properties();
        /********** Begin **********/
        PageHelper pageHelper=new PageHelper();
        properties.setProperty("helperDialect","mysql");
        properties.setProperty("supportMethodsArguments","true");
        properties.setProperty("params","count=countSql");
        pageHelper.setProperties(properties);
        return pageHelper;
        /********** End **********/
    }

}

第3关:查看博客详情

BlogMapper2.java

package net.educoder.mapper;

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

@Mapper
public interface BlogMapper2 {

    /********** Begin **********/

    /**
     * 通过博客ID 查询博客详情
     *
     * @param blogId 博客ID
     * @return 返回博客详情
     */
    @Select("select * from t_blog where blogId = #{blogId}")
    TBlog findBlogDetail(String blogId);

    /**
     * 通过用户ID 查询用户信息
     *
     * @param userId 用户ID
     * @return 返回用户信息
     */
    @Select("select * from t_user where userId = #{userId}")
    TUser findUserInfoByUserId(long userId);


    /********** End **********/
}

BlogServiceImpl2.java

package net.educoder.service.impl;

import net.educoder.entity.TBlog;
import net.educoder.entity.TUser;
import net.educoder.mapper.BlogMapper2;
import net.educoder.service.BlogService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


@Service
@Transactional
public class BlogServiceImpl2 implements BlogService2 {


    /**
     * BlogMapper2 对象注入
     */
    @Autowired
    private BlogMapper2 mapper;

    /**
     * 查看博客详情
     *
     * @param BlogId 博客ID
     * @return Map。博客信息存储在 Map 内,key 为 blogData ;博客的编写者信息(用户信息)存储到 Map 内,key 为 userInfo
     */
    @Override
    public Map<String, Object> findBlogDetail(String BlogId) {
        Map<String, Object> map = new HashMap<>();
        /********** Begin **********/
        //1.查询博客详情
        TBlog blog = mapper.findBlogDetail(BlogId);
        //2.获取博客信息中的用户ID
        long userId = blog.getUserId();
        //3.查询用户信息
        TUser user = mapper.findUserInfoByUserId(userId);
        //4.map 数据存储
        map.put("blogData", blog);
        map.put("userInfo", user);
        /********** End **********/
        return map;
    }
}

Step3Controller.java

package net.educoder.controller;

import net.educoder.service.BlogService2;
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 Step3Controller {


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


    /**
     * 博客详情
     *
     * @param blogId 博客ID
     * @return JSON
     */
    @RequestMapping("/findBlogDetail")
    @ResponseBody
    public Map<String, Object> findBlogDetail(String blogId) {
        /********** Begin **********/
        //查询博客详情并返回给前端
        Map<String, Object> map = service.findBlogDetail(blogId);
        return map;
        /********** End **********/
    }
}

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值