blog保姆級別開發流程七

木子津發表於2020-11-20

< 2020-10-7

部落格分類 types.html

<div class="ui labeled button m-margin-tb-tiny" th:each="type : ${types}">
  <a href="#" th:href="@{/types/{id}(id=${type.id})}" class="ui basic  button" th:classappend="${type.id==activeTypeId} ? 'teal'" th:text="${type.name}">思考與感悟</a>
  <div class="ui basic  left pointing label" th:classappend="${type.id==activeTypeId} ? 'teal'" th:text="${#arrays.length(type.blogs)}">24</div>
</div>
      <div class="ui labeled button m-margin-tb-tiny" th:each="type : ${types}">
        <a href="#" th:href="@{/types/{id}(id=${type.id})}" class="ui basic  button" th:classappend="${type.id==activeTypeId} ? 'teal'" th:text="${type.name}">思考與感悟</a>
        <div class="ui basic  left pointing label" th:classappend="${type.id==activeTypeId} ? 'teal'" th:text="${#arrays.length(type.blogs)}">24</div>
      </div>

**BlogQuery:**Blog封裝條件類

package com.lj.vo;

/**
 * @author lj
 * @Description: Blog封裝條件類
 * @date 2020-10-03 15:22
 * @QQ 851477108
 */

//為了解決blog分頁條件新增type空指標
public class BlogQuery {

    private String title;
    private Long typeId;
    private boolean recommend;

    public BlogQuery() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getTypeId() {
        return typeId;
    }

    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }
}

TypeShowController

package com.lj.controller;

import com.lj.po.Type;
import com.lj.service.BlogService;
import com.lj.service.TypeService;
import com.lj.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author lj
 * @Description: TypeShowController
 * @date 2020-10-05 15:56
 * @QQ 851477108
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    /**
     * 分類查詢,進入分類功能,列出所有分類,分頁查詢第一個分類下所有部落格
     * @param pageable
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 4, sort = {"views"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Long id, Model model) {
        List<Type> types = typeService.listTypeTop(10000);
        if (id == -1) {
           id = types.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("types", types);
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}

TypeService

package com.lj.service;

import com.lj.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * @author lj
 * @Description: TypeService
 * @date 2020-10-02 16:05
 * @QQ 851477108
 */
public interface TypeService {

    Type saveType(Type type);

    Type getType(Long id);

    Type getTypeByName(String name);

    Page<Type> listType(Pageable pageable);

    List<Type> listType();

    List<Type> listTypeTop(Integer size);

    Type updateType(Long id,Type type);

    void deleteType(Long id);
}

TypeServiceImpl

package com.lj.service;

import com.lj.NotFoundException;
import com.lj.dao.TypeRepository;
import com.lj.po.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lj
 * @Description: TypeServiceImpl
 * @date 2020-10-02 16:05
 * @QQ 851477108
 */
@Service
@Transactional
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeRepository typeRepository;

    /**
     * 儲存新分類
     * @param type
     * @return
     */
    @Override
    public Type saveType(Type type) {
        return typeRepository.save(type);
    }

    /**
     * 根據id查詢分類
     * @param id
     * @return
     */
    @Override
    public Type getType(Long id) {
        return typeRepository.findById(id).orElse(null);
    }

    /**
     * 根據name查詢分類
     * @param name
     */
    @Override
    public Type getTypeByName(String name) {
        return typeRepository.findByName(name);
    }

    /**
     * 分頁查詢分類
     * @param pageable
     * @return
     */
    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }

    /**
     * 查詢所有type
     * @return
     */
    @Override
    public List<Type> listType() {
        return typeRepository.findAll();
    }
    /**
     * 查詢List<Type>,根據size,sort分頁規則列出
     * @param size
     * @return
     */
    @Override
    public List<Type> listTypeTop(Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC,"blogs.size");
        Pageable pageable = PageRequest.of(0,size,sort);
        return typeRepository.findTop(pageable);
    }
    /**
     * 根據id更新分類
     * @param id
     * @param type
     * @return
     */
    @Override
    public Type updateType(Long id, Type type) {
        Type t = typeRepository.findById(id).orElse(null);
        if (t == null) {
            throw new NotFoundException("不存在該型別");
        }
        BeanUtils.copyProperties(type,t);
        return typeRepository.save(t);
    }


    /**
     * 根據id刪除分類
     * @param id
     */
    @Override
    public void deleteType(Long id) {
        typeRepository.deleteById(id);
    }
}

TypeRepository

package com.lj.dao;

import com.lj.po.Type;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author lj
 * @Description: UserRepository
 * @date 2020-10-02 12:29
 * @QQ 851477108
 */
public interface TypeRepository extends JpaRepository<Type,Long> {

    Type findByName(String name);


    @Query("select t from Type t")
    List<Type> findTop(Pageable pageable);
}

部落格標籤

tags.html

<a href="#" th:href="@{/tags/{id}(id=${tag.id})}"  class="ui basic left pointing large label m-margin-tb-tiny" th:classappend="${tag.id==activeTagId} ? 'teal'" th:each="tag : ${tags}">
  <span th:text="${tag.name}">方法論</span> <div class="detail" th:text="${#arrays.length(tag.blogs)}">23</div>
</a>
        <div class="ui padded vertical segment m-padded-tb-large" th:each="blog : ${page.content}">
              <h3 class="ui header" ><a href="#" th:href="@{/blog/{id}(id=${blog.id})}" target="_blank" class="m-black" th:text="${blog.title}"> </a></h3>
              <p class="m-text" th:text="|${blog.description}......|"> </p>
                      <div class="item">
                        <i class="calendar icon"></i><span th:text="${#dates.format(blog.updateTime,'yyyy-MM-dd')}">2017-10-01</span>
                      </div>
                      <div class="item">
                        <i class="eye icon"></i> <span th:text="${blog.views}">2342</span>
                      </div>

TagShowController

package com.lj.controller;
import com.lj.po.Tag;
import com.lj.service.BlogService;
import com.lj.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author lj
 * @Description: TagShowController
 * @date 2020-10-05 15:56
 * @QQ 851477108
 */
@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    /**
     * 分類查詢,進入分類功能,列出所有分類,分頁查詢第一個分類下所有部落格
     * @param pageable
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size = 4, sort = {"views"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Long id, Model model) {
        List<Tag> tags = tagService.listTagTop(10000);
        if (id == -1) {
           id = tags.get(0).getId();
        }
        model.addAttribute("tags", tags);
        model.addAttribute("page", blogService.listBlog(id,pageable));
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}

TagService.java

package com.lj.service;

import com.lj.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * @author lj
 * @Description: TagService
 * @date 2020-10-03 12:48
 * @QQ 851477108
 */
public interface TagService {

    Tag saveTag(Tag type);

    Tag getTag(Long id);

    Tag getTagByName(String name);

    Page<Tag> listTag(Pageable pageable);

    List<Tag> listTag();

    List<Tag> listTagTop(Integer size);

    List<Tag> listTag(String ids);

    Tag updateTag(Long id, Tag type);

    void deleteTag(Long id);
}

TagServiceImpl

package com.lj.service;

import com.lj.NotFoundException;
import com.lj.dao.TagRepository;
import com.lj.po.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lj
 * @Description: TagServiceImpl
 * @date 2020-10-03 12:49
 * @QQ 851477108
 */
@Service
@Transactional
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository tagRepository;

    /**
     * 儲存標籤
     * @param tag
     * @return
     */
    @Override
    public Tag saveTag(Tag tag) {
        return tagRepository.save(tag);
    }

    /**
     * 根據id查詢標籤
     * @param id
     * @return
     */
    @Override
    public Tag getTag(Long id) {
        return tagRepository.findById(id).orElse(null);
    }
    /**
     * 根據name查詢標籤
     * @param name
     * @return
     */
    @Override
    public Tag getTagByName(String name) {
        return tagRepository.findByName(name);
    }

    /**
     * 分類查詢標籤
     * @param pageable
     * @return
     */
    @Override
    public Page<Tag> listTag(Pageable pageable) {
        return tagRepository.findAll(pageable);
    }
    /**
     * 查詢所有標籤
     * @return
     */
    @Override
    public List<Tag> listTag() {
        return tagRepository.findAll();
    }

    /**
     * 查詢List<Tag>,根據size,sort分頁規則列出
     * @param size
     * @return
     */
    @Override
    public List<Tag> listTagTop(Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "blogs.size");
        Pageable pageable = PageRequest.of(0, size, sort);
        return tagRepository.findTop(pageable);
    }

    /**
     *將ids轉換成list後查詢所有標籤
     * @param ids
     * @return
     */
    @Override
    public List<Tag> listTag(String ids) { //1,2,3
        return tagRepository.findAllById(convertToList(ids));
    }
    /**
     * 將String型別ids用Long方法轉換成陣列列表
     * @param ids
     * @return
     */
    private List<Long> convertToList(String ids) {
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idarray = ids.split(",");
            for (String s : idarray) {
                list.add(new Long(s));
            }
        }
        return list;
    }


    /**
     * 根據id更新標籤
     * @param id
     * @param tag
     * @return
     */
    @Override
    public Tag updateTag(Long id, Tag tag) {
        Tag t = tagRepository.findById(id).orElse(null);
        if (t == null) {
            throw new NotFoundException("不存在該標籤");
        }
        BeanUtils.copyProperties(tag,t);
        return tagRepository.save(t);
    }

    /**
     * 根據id刪除
     * @param id
     */
    @Override
    public void deleteTag(Long id) {
        tagRepository.deleteById(id);
    }
}

TagRepository

package com.lj.dao;

import com.lj.po.Tag;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author lj
 * @Description: TagRepository
 * @date 2020-10-03 12:50
 * @QQ 851477108
 */
public interface TagRepository extends JpaRepository<Tag,Long> {

    Tag findByName(String name);

    @Query("select t from Tag t")
    List<Tag> findTop(Pageable pageable);
}

log開發流程七,部落格前臺分類和前臺標籤,注意的是與首頁分類和標籤對應

相關文章