Java之Spring Boot+Vue+Element UI前后端分离项目(中-功能完善-实现查询) 【博客论坛项目高仿CSDN】(一篇文章精通系列)

x33g5p2x  于2022-03-06 转载在 Java  
字(16.8k)|赞(0)|评价(0)|浏览(421)

一 、实现查询博客功能(后端功能实现)

1、完善后端代码实现查询博客信息并实现分页查询

(1)创建BlogService和BlogServiceImpl

BlogService

  1. package cn.itbluebox.springbootcsdn.service;
  2. public interface BlogService {
  3. }

BlogServiceImpl

  1. package cn.itbluebox.springbootcsdn.service.Impl;
  2. import cn.itbluebox.springbootcsdn.service.BlogService;
  3. @Service
  4. public class BlogServiceImpl implements BlogService {
  5. }
(2)编写BlogController

  1. package cn.itbluebox.springbootcsdn.web;
  2. import cn.itbluebox.springbootcsdn.domain.Blog;
  3. import cn.itbluebox.springbootcsdn.service.BlogService;
  4. import cn.itbluebox.springbootcsdn.vo.PageResult;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.RestController;
  11. @RestController
  12. @RequestMapping("blog")
  13. public class BlogController {
  14. @Autowired
  15. private BlogService blogService;
  16. @GetMapping("queryBlogByPage")
  17. public ResponseEntity<PageResult<Blog>> queryBlogByPage(
  18. @RequestParam(value = "title", defaultValue = "") String title,
  19. @RequestParam(value = "page", defaultValue = "1") Integer page,
  20. @RequestParam(value = "rows", defaultValue = "5") Integer rows
  21. ) {
  22. System.out.println(title + page + rows);
  23. return ResponseEntity.ok(blogService.queryBlogByPage(title, page, rows));
  24. }
  25. }

(3)完善BlogService 和BlogServiceImpl

BlogService

  1. package cn.itbluebox.springbootcsdn.service;
  2. import cn.itbluebox.springbootcsdn.domain.Blog;
  3. import cn.itbluebox.springbootcsdn.vo.PageResult;
  4. public interface BlogService {
  5. PageResult<Blog> queryBlogByPage(String title, Integer page, Integer rows);
  6. }

BlogServiceImpl

  1. package cn.itbluebox.springbootcsdn.service.Impl;
  2. import cn.itbluebox.springbootcsdn.domain.Blog;
  3. import cn.itbluebox.springbootcsdn.mapper.BlogMapper;
  4. import cn.itbluebox.springbootcsdn.service.BlogService;
  5. import cn.itbluebox.springbootcsdn.vo.PageResult;
  6. import com.github.pagehelper.PageHelper;
  7. import com.github.pagehelper.PageInfo;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. @Service
  12. public class BlogServiceImpl implements BlogService {
  13. @Autowired
  14. private BlogMapper blogMapper;
  15. @Override
  16. public PageResult<Blog> queryBlogByPage(String title, Integer page, Integer rows) {
  17. PageHelper.startPage(page, rows);//自动创建好分页的条件
  18. System.out.println("----------");
  19. List<Blog> list = blogMapper.queryBlogByPage(title);
  20. PageResult pageResult = new PageResult();
  21. pageResult.setItems(list);//设置数据
  22. //解析分页结果
  23. PageInfo<Blog> pageInfo = new PageInfo<Blog>(list);//得到分页信息
  24. pageResult.setTotal(pageInfo.getTotal());//设置总条数
  25. long l = pageInfo.getTotal() / pageInfo.getPageSize();
  26. pageResult.setTotalPage(Integer.parseInt(l+1+""));
  27. return pageResult;
  28. }
  29. }
(4)完善BlogMapper

  1. package cn.itbluebox.springbootcsdn.mapper;
  2. import cn.itbluebox.springbootcsdn.domain.Blog;
  3. import org.apache.ibatis.annotations.Select;
  4. import tk.mybatis.mapper.common.Mapper;
  5. import java.util.List;
  6. public interface BlogMapper extends Mapper<Blog> {
  7. @Select("select * from blog where title like '%${title}%'")
  8. List<Blog> queryBlogByPage(String title);
  9. }
(5)完善SpringBootCSDNApplication

  1. package cn.itbluebox.springbootcsdn;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import tk.mybatis.spring.annotation.MapperScan;
  5. @SpringBootApplication
  6. @MapperScan("cn.itbluebox.springbootcsdn.mapper")
  7. public class SpringBootCSDNApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringBootCSDNApplication.class, args);
  10. }
  11. }

2、运行测试

访问:http://localhost:9090/blog/queryBlogByPage?title=&page=1&rows=5

二 、实现查询博客功能(前端功能实现)

1、修改App.vue

2、完善HelloWorld.vue

  1. <template>
  2. <div>
  3. <el-table
  4. :data="items"
  5. @row-click="open"
  6. style="width: 80%;margin: auto;top:80px">
  7. <el-table-column
  8. prop="thumbnail"
  9. label="缩略图"
  10. width="180">
  11. <!-- 图片的显示 -->
  12. <template slot-scope="scope">
  13. <img :src="scope.row.thumbnail" min-width="70" height="70" />
  14. </template>
  15. </el-table-column>
  16. <el-table-column
  17. prop="title"
  18. label="标题"
  19. width="180">
  20. </el-table-column>
  21. <el-table-column
  22. prop="abstract_text"
  23. label="摘要">
  24. </el-table-column>
  25. </el-table>
  26. <div style="height: 500px;margin-top: 100px ">
  27. <el-pagination
  28. layout="prev, pager, next"
  29. :total="total"
  30. @current-change="current_change"
  31. >
  32. </el-pagination>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. name: 'HelloWorld',
  39. data() {
  40. return {
  41. info: "",
  42. total: 0,
  43. totalPage: 0,
  44. items: [],
  45. title: "",
  46. page: 1,
  47. rows:5,
  48. first: 1,
  49. last: 5,
  50. startBlogTime: "",
  51. endBlogTime: "",
  52. id: "",
  53. name: "请登录",
  54. image: "",
  55. email: "",
  56. }
  57. },
  58. created() {//编写构造函数
  59. this.getInfo();
  60. },
  61. methods: {
  62. getInfo() {
  63. this.$axios.get('http://localhost:9090/blog/queryBlogByPage?title=' + this.title + '&page=' + this.page + '&rows=' + this.rows)
  64. .then(response => (
  65. this.info = response.data,
  66. this.total = this.info.total,
  67. this.totalPage = this.info.totalPage,
  68. this.items = this.info.items
  69. )).catch(function (error) { // 请求失败处理
  70. console.log(error);
  71. });
  72. },
  73. current_change:function(currentPage){
  74. this.page = currentPage;
  75. this.getInfo();
  76. }
  77. },
  78. watch: {
  79. page: function () {
  80. this.getInfo();
  81. },
  82. rows: function () {
  83. this.getInfo();
  84. },
  85. }
  86. }
  87. </script>
  88. <style scoped>
  89. .el-row {
  90. margin-bottom: 20px;
  91. }
  92. .el-col {
  93. border-radius: 4px;
  94. }
  95. .bg-purple-dark {
  96. background: #99a9bf;
  97. }
  98. .bg-purple {
  99. background: #d3dce6;
  100. }
  101. .bg-purple-light {
  102. background: #e5e9f2;
  103. }
  104. .grid-content {
  105. border-radius: 4px;
  106. min-height: 36px;
  107. }
  108. .row-bg {
  109. padding: 10px 0;
  110. background-color: #f9fafc;
  111. }
  112. #top {
  113. position: fixed;
  114. top: 0px;
  115. height: 50px;
  116. width: 100%;
  117. background-color: #f9fafc;
  118. height: 80px;
  119. z-index: 1;
  120. box-shadow: 2px 2px 5px #888888;
  121. left: 0px;
  122. }
  123. #top1 {
  124. width: 70%;
  125. margin: auto;
  126. padding-top: 20px;
  127. }
  128. </style>

访问http://localhost:8080/#/

点击翻页

三 、实现查询博客详情(后端功能实现)

1、修改BlogArticle

  1. package cn.itbluebox.springbootcsdn.domain;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import javax.persistence.Table;
  6. import java.util.Date;
  7. @Data
  8. @NoArgsConstructor
  9. @AllArgsConstructor
  10. @Table(name = "blog_article")
  11. public class BlogArticle extends Blog{
  12. private Long id;
  13. private String context;
  14. private Date last_update_time; //更新时间
  15. private Character is_original;
  16. }

2、完善BlogController实现通过id查询

  1. @GetMapping("queryBlogArticleById")
  2. public ResponseEntity<BlogArticle> queryBlogById(
  3. @RequestParam(value = "id") Long id
  4. ) {
  5. return ResponseEntity.ok(blogService.queryBlogArticleById(id));
  6. }

3、完善BlogService以及BlogServiceImpl

BlogService

  1. package cn.itbluebox.springbootcsdn.service;
  2. import cn.itbluebox.springbootcsdn.domain.Blog;
  3. import cn.itbluebox.springbootcsdn.domain.BlogArticle;
  4. import cn.itbluebox.springbootcsdn.vo.PageResult;
  5. public interface BlogService {
  6. PageResult<Blog> queryBlogByPage(String title, Integer page, Integer rows);
  7. BlogArticle queryBlogArticleById(Long id);
  8. }

BlogServiceImpl

  1. @Override
  2. public BlogArticle queryBlogArticleById(Long id) {
  3. return blogArticleMapper.queryBlogArticleById(id);
  4. }

4、完善BlogArticleMapper

  1. package cn.itbluebox.springbootcsdn.mapper;
  2. import cn.itbluebox.springbootcsdn.domain.BlogArticle;
  3. import org.apache.ibatis.annotations.Select;
  4. import tk.mybatis.mapper.common.Mapper;
  5. public interface BlogArticleMapper extends Mapper<BlogArticle> {
  6. @Select("select * from blog_article ba LEFT JOIN blog b on ba.id = b.blog_article_id where ba.id = #{id} LIMIT 0,1")
  7. BlogArticle queryBlogArticleById(Long id);
  8. }

5、运行测试

访问:http://localhost:9090/blog/queryBlogArticleById?id=1

三 、实现查询博客详情(前端功能实现)

1、在HelloWorld.vue当中设置跳转页面的方法并携带参数

  1. open(row) {
  2. this.$router.push("/Article?" + row.id);
  3. },

2、创建Article.vue

(1)实现通过id查询对应的详细内容

  1. <template>
  2. <div style="width: 100%;background-color: #99a9bf ">
  3. <div style="width: 80%;margin: auto;background-color: #f9fafc">
  4. <el-row style="position: fixed; top: 0px;background-color: #2c3e50 ;width: 80%; z-index: 1" >
  5. <el-col :span="24" style="background-color: white">
  6. <div style="text-align: left;box-shadow: 5px 10px 15px 2px rgba(0,0,0,0.1);height: 50px;line-height: 50px;font-weight: bold;padding-left: 10px;background-color: #f9fafc"
  7. >
  8. <span v-html="title"></span>
  9. <span style="margin-left: 50px" > 浏览量:<span v-html="info.view_count"></span></span>
  10. </div>
  11. </el-col>
  12. </el-row>
  13. <el-row>
  14. <el-col :span="12"><div class="grid-content bg-purple"></div></el-col>
  15. <el-col :span="12"><div class="grid-content bg-purple-light"></div></el-col>
  16. </el-row>
  17. <el-row>
  18. <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
  19. <el-col :span="8"><div class="grid-content bg-purple-light"></div></el-col>
  20. <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
  21. </el-row>
  22. <el-row>
  23. <el-col :span="24" style="margin-top: 80px;">
  24. <div style="padding: 50px">
  25. <p style="text-align: left" v-html="info.context"></p>
  26. </div>
  27. </el-col>
  28. </el-row>
  29. <el-row style="position: fixed; bottom : 0px;background-color: #2c3e50 ;width: 80%; z-index: 1" >
  30. <el-col :span="24" style="background-color: white">
  31. <div style="text-align: left;box-shadow: 5px 10px 15px 2px rgba(0,0,0,0.1);height: 50px;line-height: 30px;font-weight: bold;padding-left: 20px;background-color: #f9fafc"
  32. >
  33. <span style="margin-left: 50px" > 点赞数:<span v-html="info.like_count"></span></span>
  34. <span style="float:right;margin-right: 150px">
  35. <img height="50" @click="like" src="https://img2.baidu.com/it/u=966753824,2436291344&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500">
  36. </span>
  37. </div>
  38. </el-col>
  39. </el-row>
  40. </div>
  41. </div>
  42. </template>
  43. <script>
  44. export default {
  45. name: "Article",
  46. data() {
  47. return {
  48. id:"",
  49. info: "",
  50. total: 0,
  51. totalPage: 0,
  52. items: [],
  53. title: "",
  54. page: 1,
  55. rows: 10,
  56. first: 1,
  57. last: 10,
  58. startBlogTime: "",
  59. endBlogTime: "",
  60. }
  61. },
  62. created() {//编写构造函数
  63. this.id = location.href.split("?")[1];
  64. this.getInfo();
  65. },
  66. methods: {
  67. getInfo() {
  68. this.$axios.get('http://localhost:9090/blog/queryBlogArticleById?id=' + this.id )
  69. .then(response => (
  70. this.info = response.data,
  71. this.title = this.info.title
  72. )).catch(function (error) { // 请求失败处理
  73. console.log(error);
  74. });
  75. },
  76. selectBlog() {
  77. this.page = 1;
  78. this.rows = 10;
  79. let startTime = (new Date(((this.value1+"").split(",")[0]))).getTime();
  80. let endTime = (new Date(((this.value1+"").split(",")[1]))).getTime();
  81. this.startBlogTime = startTime;
  82. this.endBlogTime = endTime;
  83. this.getInfo();
  84. },
  85. like(){
  86. this.$axios.get('http://localhost:9090/blogging/blogLikeId?id=' + this.id );
  87. this.getInfoView();
  88. },
  89. current_change:function(currentPage){
  90. this.page = currentPage;
  91. this.getInfo();
  92. },
  93. },
  94. watch: {
  95. page: function () {
  96. this.getInfo();
  97. },
  98. rows: function () {
  99. this.getInfo();
  100. },
  101. }
  102. }
  103. </script>
  104. <style scoped>
  105. </style>
(2)在router下的index.js当中设置页面跳转路径

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import HelloWorld from '@/components/HelloWorld'
  4. import Article from '@/components/Article'
  5. Vue.use(Router)
  6. export default new Router({
  7. routes: [
  8. {
  9. path: '/',
  10. name: 'HelloWorld',
  11. component: HelloWorld
  12. },
  13. {
  14. path: '/Article',
  15. name: 'Article',
  16. component: Article
  17. },
  18. ]
  19. })
(3)点击测试

3、实现浏览量增加

实现浏览量的增加

(1)修改BlogController

  1. @GetMapping("queryBlogArticleById")
  2. public ResponseEntity<BlogArticle> queryBlogById(
  3. @RequestParam(value = "id") Long id
  4. ) {
  5. //查询当前id 对应的博客信息
  6. BlogArticle blogArticle = blogService.queryBlogArticleById(id);
  7. Blog blog = blogService.queryBlogById(blogArticle.getId());
  8. Long view_count = blog.getView_count();
  9. //将访问量查询并自增后重新设置值
  10. view_count = view_count + 1;
  11. blog.setView_count(view_count);
  12. //更新数据库大当中的值
  13. blogService.updateBlog(blog);
  14. return ResponseEntity.ok(blogArticle);
  15. }
(2)对应的实现的查询和更新的接口和实现类

  1. Blog queryBlogById(Long id);
  2. void updateBlog(Blog blog);

  1. @Override
  2. public Blog queryBlogById(Long id) {
  3. return blogMapper.queryBlogById(id);
  4. }
  5. @Transactional(rollbackFor = Exception.class)
  6. public void updateBlog(Blog blog) {
  7. blogMapper.updateByView(blog);
  8. }
(2)对应的BlogMapper

  1. @Select("select * from blog where id = #{id} limit 0,1")
  2. Blog queryBlogById(Long id);
  3. @Update("UPDATE blog set view_count = #{view_count} WHERE id = #{id}")
  4. void updateByView(Blog blog);

4、实现点赞

(1)完善Article.vue设置事件和请求方式

对应的方法

放置手残实现全部代码

  1. <template>
  2. <div style="width: 100%;background-color: #99a9bf ">
  3. <div style="width: 80%;margin: auto;background-color: #f9fafc">
  4. <el-row style="position: fixed; top: 0px;background-color: #2c3e50 ;width: 80%; z-index: 1" >
  5. <el-col :span="24" style="background-color: white">
  6. <div style="text-align: left;box-shadow: 5px 10px 15px 2px rgba(0,0,0,0.1);height: 50px;line-height: 50px;font-weight: bold;padding-left: 10px;background-color: #f9fafc"
  7. >
  8. <span v-html="title"></span>
  9. <span style="margin-left: 50px" > 浏览量:<span v-html="info.view_count"></span></span>
  10. </div>
  11. </el-col>
  12. </el-row>
  13. <el-row>
  14. <el-col :span="12"><div class="grid-content bg-purple"></div></el-col>
  15. <el-col :span="12"><div class="grid-content bg-purple-light"></div></el-col>
  16. </el-row>
  17. <el-row>
  18. <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
  19. <el-col :span="8"><div class="grid-content bg-purple-light"></div></el-col>
  20. <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
  21. </el-row>
  22. <el-row>
  23. <el-col :span="24" style="margin-top: 80px;">
  24. <div style="padding: 50px">
  25. <p style="text-align: left" v-html="info.context"></p>
  26. </div>
  27. </el-col>
  28. </el-row>
  29. <el-row style="position: fixed; bottom : 0px;background-color: #2c3e50 ;width: 80%; z-index: 1" >
  30. <el-col :span="24" style="background-color: white">
  31. <div style="text-align: left;box-shadow: 5px 10px 15px 2px rgba(0,0,0,0.1);height: 50px;line-height: 30px;font-weight: bold;padding-left: 20px;background-color: #f9fafc"
  32. >
  33. <span style="margin-left: 50px" > 点赞数:<span v-html="info.like_count"></span></span>
  34. <span style="float:right;margin-right: 150px">
  35. <img height="50" @click="like" src="https://img2.baidu.com/it/u=966753824,2436291344&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500">
  36. </span>
  37. </div>
  38. </el-col>
  39. </el-row>
  40. </div>
  41. </div>
  42. </template>
  43. <script>
  44. export default {
  45. name: "Article",
  46. data() {
  47. return {
  48. id:"",
  49. info: "",
  50. total: 0,
  51. totalPage: 0,
  52. items: [],
  53. title: "",
  54. page: 1,
  55. rows: 10,
  56. first: 1,
  57. last: 10,
  58. startBlogTime: "",
  59. endBlogTime: "",
  60. }
  61. },
  62. created() {//编写构造函数
  63. this.id = location.href.split("?")[1];
  64. this.getInfo();
  65. },
  66. methods: {
  67. getInfo() {
  68. this.$axios.get('http://localhost:9090/blog/queryBlogArticleById?id=' + this.id )
  69. .then(response => (
  70. this.info = response.data,
  71. this.title = this.info.title
  72. )).catch(function (error) { // 请求失败处理
  73. console.log(error);
  74. });
  75. },
  76. selectBlog() {
  77. this.page = 1;
  78. this.rows = 10;
  79. let startTime = (new Date(((this.value1+"").split(",")[0]))).getTime();
  80. let endTime = (new Date(((this.value1+"").split(",")[1]))).getTime();
  81. this.startBlogTime = startTime;
  82. this.endBlogTime = endTime;
  83. this.getInfo();
  84. },
  85. like(){
  86. this.$axios.get('http://localhost:9090/blog/blogLikeId?id=' + this.id );
  87. this.getInfo();
  88. },
  89. current_change:function(currentPage){
  90. this.page = currentPage;
  91. this.getInfo();
  92. },
  93. },
  94. watch: {
  95. page: function () {
  96. this.getInfo();
  97. },
  98. rows: function () {
  99. this.getInfo();
  100. },
  101. }
  102. }
  103. </script>
  104. <style scoped>
  105. </style>
(2)完善后端接口

BlogController

  1. @GetMapping("blogLikeId")
  2. public ResponseEntity<BlogArticle> queryBlogLikeId(
  3. @RequestParam(value = "id") Long id
  4. ) {
  5. //查询当前id 对应的博客信息
  6. BlogArticle blogArticle = blogService.queryBlogArticleById(id);
  7. Blog blog = blogService.queryBlogById(blogArticle.getId());
  8. Long like_count = blog.getLike_count();
  9. //将访问量查询并自增后重新设置值
  10. like_count = like_count + 1;
  11. blog.setLike_count(like_count);
  12. //更新数据库大当中的值
  13. blogService.updateBlogLikeCount(blog);
  14. return ResponseEntity.ok(blogArticle);
  15. }

  1. void updateBlogLikeCount(Blog blog);

BlogServiceImpl

  1. @Override
  2. public void updateBlogLikeCount(Blog blog) {
  3. blogMapper.updateByLike(blog);
  4. }

BlogMapper

  1. @Update("UPDATE blog set like_count = #{like_count} WHERE id = #{id}")
  2. void updateByLike(Blog blog);

点击测试

相关文章

最新文章

更多