diff --git a/pom.xml b/pom.xml
index 9eaf4c4..5699830 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,6 +59,15 @@
${lombok.version}
provided
+
+
+ org.springframework.boot
+ spring-boot-starter-cache
+
+
+ net.sf.ehcache
+ ehcache
+
diff --git a/src/main/java/cc/bnblogs/Application.java b/src/main/java/cc/bnblogs/Application.java
index 1a55733..7c7ce49 100644
--- a/src/main/java/cc/bnblogs/Application.java
+++ b/src/main/java/cc/bnblogs/Application.java
@@ -2,11 +2,14 @@ package cc.bnblogs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
+
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/16
*/
+@EnableCaching
@SpringBootApplication
public class Application {
public static void main(String[] args) {
diff --git a/src/main/java/cc/bnblogs/common/DefaultImages.java b/src/main/java/cc/bnblogs/common/DefaultImages.java
new file mode 100644
index 0000000..87e206d
--- /dev/null
+++ b/src/main/java/cc/bnblogs/common/DefaultImages.java
@@ -0,0 +1,27 @@
+package cc.bnblogs.common;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+import org.thymeleaf.util.StringUtils;
+
+import java.util.List;
+
+/**
+ * @author zfp@bnblogs.cc
+ * @createTime: 2022/10/20
+ */
+@Component
+@Data
+@ConfigurationProperties(prefix = "default-images")
+public class DefaultImages {
+ private List images;
+
+ public String cover(String imgUrl) {
+ if (StringUtils.isEmptyOrWhitespace(imgUrl)) {
+ // 返回一张随机图
+ return images.get((int)(Math.random() * images.size()));
+ }
+ return imgUrl;
+ }
+}
diff --git a/src/main/java/cc/bnblogs/config/CacheConfig.java b/src/main/java/cc/bnblogs/config/CacheConfig.java
new file mode 100644
index 0000000..751de0b
--- /dev/null
+++ b/src/main/java/cc/bnblogs/config/CacheConfig.java
@@ -0,0 +1,52 @@
+package cc.bnblogs.config;
+
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.interceptor.KeyGenerator;
+import org.springframework.context.annotation.Configuration;
+import org.thymeleaf.util.StringUtils;
+
+import java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * @author zfp@bnblogs.cc
+ * @createTime: 2022/10/20
+ */
+@Configuration
+public class CacheConfig extends CachingConfigurerSupport {
+ @Override
+ public KeyGenerator keyGenerator() {
+ return (target, method, params) -> new BlogCacheKey(target.getClass().getName(), method.getName(), params);
+ }
+
+ static class BlogCacheKey implements Serializable {
+ private final String className;
+ private final String methodName;
+ private final Object[] params;
+ private final int hashCode;
+
+ public BlogCacheKey(String className, String methodName, Object[] params) {
+ this.className = className;
+ this.methodName = methodName;
+ this.params = params;
+ String sign = className + "_" + methodName + "_" + Arrays.deepHashCode(params);
+ this.hashCode = sign.hashCode();
+ }
+
+ @Override
+ public int hashCode() {
+ return this.hashCode;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) return true;
+ if (!(obj instanceof BlogCacheKey)) return false;
+ BlogCacheKey other = (BlogCacheKey) obj;
+ if (this.hashCode == other.hashCode) return true;
+ return StringUtils.equals(this.className, other.className)
+ && StringUtils.equals(this.methodName, other.methodName)
+ && Arrays.deepEquals(this.params, other.params);
+ }
+ }
+}
diff --git a/src/main/java/cc/bnblogs/config/ExceptionConfig.java b/src/main/java/cc/bnblogs/config/ExceptionConfig.java
index 499ed68..fd9ca3e 100644
--- a/src/main/java/cc/bnblogs/config/ExceptionConfig.java
+++ b/src/main/java/cc/bnblogs/config/ExceptionConfig.java
@@ -1,6 +1,7 @@
package cc.bnblogs.config;
import cc.bnblogs.common.Result;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -9,12 +10,15 @@ import org.springframework.web.bind.annotation.ResponseBody;
* @author zfp@bnblogs.cc
* @createTime: 2022/10/19
*/
+@Slf4j
@ControllerAdvice
public class ExceptionConfig {
@ResponseBody
@ExceptionHandler(Exception.class)
- public Result defaultException() {
+ public Result defaultException(Exception e) {
+ e.printStackTrace();
+ log.error(e.getMessage(),e);
return Result.error();
}
}
diff --git a/src/main/java/cc/bnblogs/controller/AdminController.java b/src/main/java/cc/bnblogs/controller/AdminController.java
index 5fe4a01..928a6dd 100644
--- a/src/main/java/cc/bnblogs/controller/AdminController.java
+++ b/src/main/java/cc/bnblogs/controller/AdminController.java
@@ -74,7 +74,7 @@ public class AdminController {
Article article = Objects.isNull(id) ? Article.builder().allowComment(1).build() : articleService.detail(id);
model.addAttribute("categories",categories);
model.addAttribute("article",article);
- model.addAttribute("title", Objects.isNull(article.getId()) ? "创建新文章" : "编辑文章<" + article.getTitle() + '>');
+ model.addAttribute("pageTitle", Objects.isNull(article.getId()) ? "创建新文章" : "编辑文章---" + article.getTitle());
return "admin/write";
}
}
diff --git a/src/main/java/cc/bnblogs/controller/IndexController.java b/src/main/java/cc/bnblogs/controller/IndexController.java
index c2740ed..5ac4f76 100644
--- a/src/main/java/cc/bnblogs/controller/IndexController.java
+++ b/src/main/java/cc/bnblogs/controller/IndexController.java
@@ -1,7 +1,10 @@
package cc.bnblogs.controller;
+import cc.bnblogs.service.*;
import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
/**
@@ -10,9 +13,38 @@ import org.springframework.web.bind.annotation.PathVariable;
*/
@Controller
public class IndexController {
+
+ private final FriendsService friendsService;
+ private final NavigationService navigationService;
+ private final TagService tagService;
+ private final ArticleService articleService;
+ private final BannerService bannerService;
+
+
+ public IndexController(FriendsService friendsService, NavigationService navigationService,
+ TagService tagService, ArticleService articleService, BannerService bannerService) {
+ this.friendsService = friendsService;
+ this.navigationService = navigationService;
+ this.tagService = tagService;
+ this.articleService = articleService;
+ this.bannerService = bannerService;
+ }
+
+
+ @ModelAttribute
+ private void indexModel(Model model) {
+ // todo 可以在这里定义这个controller公用的model的属性
+ model.addAttribute("friends",friendsService.list());
+ model.addAttribute("navigations",navigationService.show());
+ model.addAttribute("tags",tagService.show((int)tagService.count()));
+ model.addAttribute("hots",articleService.hotList(5));
+ }
+
+
// 前台访问路由
@GetMapping("/")
- public String index() {
+ public String index(Model model) {
+ model.addAttribute("banners",bannerService.list());
return "index";
}
diff --git a/src/main/java/cc/bnblogs/mapper/ArticleMapper.java b/src/main/java/cc/bnblogs/mapper/ArticleMapper.java
index f8b0480..73eeafe 100644
--- a/src/main/java/cc/bnblogs/mapper/ArticleMapper.java
+++ b/src/main/java/cc/bnblogs/mapper/ArticleMapper.java
@@ -3,10 +3,15 @@ package cc.bnblogs.mapper;
import cc.bnblogs.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
public interface ArticleMapper extends JpaRepository, JpaSpecificationExecutor{
+ @Query(value = "select * from blog_article where type=1 and status=1 order by views limit ?1", nativeQuery = true)
+ List hotList(int limit);
}
diff --git a/src/main/java/cc/bnblogs/mapper/NavigationMapper.java b/src/main/java/cc/bnblogs/mapper/NavigationMapper.java
index 08d43ac..c98ff2e 100644
--- a/src/main/java/cc/bnblogs/mapper/NavigationMapper.java
+++ b/src/main/java/cc/bnblogs/mapper/NavigationMapper.java
@@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import javax.transaction.Transactional;
+import java.util.List;
/**
* @author zfp@bnblogs.cc
@@ -21,4 +22,7 @@ public interface NavigationMapper extends JpaRepository {
@Modifying
@Query("update Navigation set ordered = ?2 where id = ?1")
void ordered(Integer id, Integer ordered);
+
+
+ List findAllByEnableOrderByOrderedAsc(Boolean enable);
}
diff --git a/src/main/java/cc/bnblogs/mapper/TagMapper.java b/src/main/java/cc/bnblogs/mapper/TagMapper.java
index dde885d..41f392b 100644
--- a/src/main/java/cc/bnblogs/mapper/TagMapper.java
+++ b/src/main/java/cc/bnblogs/mapper/TagMapper.java
@@ -2,10 +2,26 @@ package cc.bnblogs.mapper;
import cc.bnblogs.pojo.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import java.util.List;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
-public interface TagMapper extends JpaRepository {
+public interface TagMapper extends JpaRepository {
+ @Query(value = "SELECT * FROM blog_tag\n" +
+ "WHERE\n" +
+ " id >= ( SELECT FLOOR(\n" +
+ " RAND() \n" +
+ " * (\n" +
+ " (SELECT MAX( id ) FROM blog_tag ) -\n" +
+ " (SELECT MIN( id ) FROM blog_tag )\n" +
+ " ) \n" +
+ " + ( SELECT MIN( id ) FROM blog_tag ))\n" +
+ " ) \n" +
+ "ORDER BY id LIMIT ?1",nativeQuery = true)
+ List findRandom(int limit);
}
diff --git a/src/main/java/cc/bnblogs/pojo/Article.java b/src/main/java/cc/bnblogs/pojo/Article.java
index d3b24ac..7bc9707 100644
--- a/src/main/java/cc/bnblogs/pojo/Article.java
+++ b/src/main/java/cc/bnblogs/pojo/Article.java
@@ -3,6 +3,7 @@ package cc.bnblogs.pojo;
import lombok.*;
import javax.persistence.*;
+import java.io.Serializable;
import java.util.Date;
import java.util.List;
@@ -16,7 +17,7 @@ import java.util.List;
@Builder
@Entity
@Table(name = "blog_article")
-public class Article {
+public class Article implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
diff --git a/src/main/java/cc/bnblogs/pojo/Banner.java b/src/main/java/cc/bnblogs/pojo/Banner.java
index 879c661..ab0d345 100644
--- a/src/main/java/cc/bnblogs/pojo/Banner.java
+++ b/src/main/java/cc/bnblogs/pojo/Banner.java
@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
+import java.io.Serializable;
/**
* @author zfp@bnblogs.cc
@@ -17,7 +18,7 @@ import javax.persistence.*;
@Builder
@Entity
@Table(name = "blog_banner")
-public class Banner {
+public class Banner implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
diff --git a/src/main/java/cc/bnblogs/pojo/Category.java b/src/main/java/cc/bnblogs/pojo/Category.java
index 3d5ae60..6664b30 100644
--- a/src/main/java/cc/bnblogs/pojo/Category.java
+++ b/src/main/java/cc/bnblogs/pojo/Category.java
@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
+import java.io.Serializable;
/**
* @author zfp@bnblogs.cc
@@ -17,7 +18,7 @@ import javax.persistence.*;
@NoArgsConstructor
@AllArgsConstructor
@Table(name="blog_category")
-public class Category {
+public class Category implements Serializable {
/**
* 分类id(主键)
* 主键自增
diff --git a/src/main/java/cc/bnblogs/pojo/Comment.java b/src/main/java/cc/bnblogs/pojo/Comment.java
index 2eec34f..89b88fa 100644
--- a/src/main/java/cc/bnblogs/pojo/Comment.java
+++ b/src/main/java/cc/bnblogs/pojo/Comment.java
@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
+import java.io.Serializable;
import java.util.Date;
/**
@@ -18,7 +19,7 @@ import java.util.Date;
@Builder
@Entity
@Table(name = "blog_comment")
-public class Comment {
+public class Comment implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
diff --git a/src/main/java/cc/bnblogs/pojo/Friends.java b/src/main/java/cc/bnblogs/pojo/Friends.java
index 4348eeb..0773875 100644
--- a/src/main/java/cc/bnblogs/pojo/Friends.java
+++ b/src/main/java/cc/bnblogs/pojo/Friends.java
@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
+import java.io.Serializable;
/**
* @author zfp@bnblogs.cc
@@ -17,7 +18,7 @@ import javax.persistence.*;
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "blog_friends")
-public class Friends {
+public class Friends implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
diff --git a/src/main/java/cc/bnblogs/pojo/Navigation.java b/src/main/java/cc/bnblogs/pojo/Navigation.java
index d32e1dc..bc71989 100644
--- a/src/main/java/cc/bnblogs/pojo/Navigation.java
+++ b/src/main/java/cc/bnblogs/pojo/Navigation.java
@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
+import java.io.Serializable;
/**
* @author zfp@bnblogs.cc
@@ -17,7 +18,7 @@ import javax.persistence.*;
@Builder
@Entity
@Table(name = "blog_navigation")
-public class Navigation {
+public class Navigation implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
diff --git a/src/main/java/cc/bnblogs/pojo/Tag.java b/src/main/java/cc/bnblogs/pojo/Tag.java
index 44e9053..3be3a42 100644
--- a/src/main/java/cc/bnblogs/pojo/Tag.java
+++ b/src/main/java/cc/bnblogs/pojo/Tag.java
@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
+import java.io.Serializable;
/**
* @author zfp@bnblogs.cc
@@ -17,7 +18,7 @@ import javax.persistence.*;
@Entity
@Builder
@Table(name = "blog_tag")
-public class Tag {
+public class Tag implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
diff --git a/src/main/java/cc/bnblogs/service/ArticleService.java b/src/main/java/cc/bnblogs/service/ArticleService.java
index a91016f..35651c0 100644
--- a/src/main/java/cc/bnblogs/service/ArticleService.java
+++ b/src/main/java/cc/bnblogs/service/ArticleService.java
@@ -7,6 +7,9 @@ import cc.bnblogs.mapper.TagMapper;
import cc.bnblogs.pojo.Article;
import cc.bnblogs.pojo.Category;
import cc.bnblogs.utils.UpdateUtil;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@@ -29,6 +32,7 @@ import java.util.stream.Collectors;
* @createTime: 2022/10/17
*/
@Service
+@CacheConfig(cacheNames = {"blog-cache"})
public class ArticleService {
private final TagMapper tagMapper;
private final ArticleMapper articleMapper;
@@ -67,6 +71,7 @@ public class ArticleService {
* 保存文章
* @param article 待保存的文章对象
*/
+ @CacheEvict(allEntries = true)
@Transactional(rollbackFor = {Exception.class})
public void save(Article article) {
// 修改更新时间
@@ -109,6 +114,7 @@ public class ArticleService {
* 根据id删除文章对象
* @param id 待删除文章的id
*/
+ @CacheEvict(allEntries = true)
public void delete(Integer id) {
articleMapper.deleteById(id);
}
@@ -142,4 +148,14 @@ public class ArticleService {
return PageHelper.builder().rows(articlePage.getContent()).total(articlePage.getTotalElements()).build();
}
+
+ /**
+ * 获取一定数量热门文章
+ * @param limit 最大文章数
+ * @return 返回热门文章
+ */
+ @Cacheable
+ public List hotList(int limit) {
+ return articleMapper.hotList(limit);
+ }
}
diff --git a/src/main/java/cc/bnblogs/service/BannerService.java b/src/main/java/cc/bnblogs/service/BannerService.java
index 627da82..2bbd4ad 100644
--- a/src/main/java/cc/bnblogs/service/BannerService.java
+++ b/src/main/java/cc/bnblogs/service/BannerService.java
@@ -2,6 +2,9 @@ package cc.bnblogs.service;
import cc.bnblogs.mapper.BannerMapper;
import cc.bnblogs.pojo.Banner;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@@ -12,6 +15,7 @@ import java.util.List;
* @createTime: 2022/10/17
*/
@Service
+@CacheConfig(cacheNames = {"blog-cache"})
public class BannerService {
private final BannerMapper bannerMapper;
@@ -25,6 +29,7 @@ public class BannerService {
* 按照ordered字段升序排序
* @return 轮播图列表
*/
+ @Cacheable
public List list() {
return bannerMapper.findAll(Sort.by(Sort.Direction.ASC,"ordered"));
}
@@ -50,6 +55,7 @@ public class BannerService {
* 保存轮播图
* @param banner 待保存的轮播图对象
*/
+ @CacheEvict(allEntries = true)
public void save(Banner banner) {
bannerMapper.save(banner);
}
@@ -58,10 +64,17 @@ public class BannerService {
* 根据id删除轮播图对象
* @param id 待删除轮播图的id
*/
+ @CacheEvict(allEntries = true)
public void delete(Integer id) {
bannerMapper.deleteById(id);
}
+ /**
+ * 轮播图排序
+ * @param id 轮播图id
+ * @param ordered 轮播图顺序
+ */
+ @CacheEvict(allEntries = true)
public void order(Integer id, Integer ordered) {
bannerMapper.ordered(id,ordered);
}
diff --git a/src/main/java/cc/bnblogs/service/FriendsService.java b/src/main/java/cc/bnblogs/service/FriendsService.java
index 3d28784..aa068a2 100644
--- a/src/main/java/cc/bnblogs/service/FriendsService.java
+++ b/src/main/java/cc/bnblogs/service/FriendsService.java
@@ -2,6 +2,9 @@ package cc.bnblogs.service;
import cc.bnblogs.mapper.FriendsMapper;
import cc.bnblogs.pojo.Friends;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -10,6 +13,7 @@ import java.util.List;
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
+@CacheConfig(cacheNames = {"blog-cache"})
@Service
public class FriendsService {
@@ -23,6 +27,7 @@ public class FriendsService {
* 获取所有友链
* @return 友链列表
*/
+ @Cacheable
public List list() {
return friendsMapper.findAll();
}
@@ -48,6 +53,8 @@ public class FriendsService {
* 保存友链
* @param friends 带保存的友链对象
*/
+ // 发生增删操作时删除缓存
+ @CacheEvict(allEntries = true)
public void save(Friends friends) {
friendsMapper.save(friends);
}
@@ -56,6 +63,7 @@ public class FriendsService {
* 根据id删除友链对象
* @param id 待删除友链的id
*/
+ @CacheEvict(allEntries = true)
public void delete(Integer id) {
friendsMapper.deleteById(id);
}
diff --git a/src/main/java/cc/bnblogs/service/NavigationService.java b/src/main/java/cc/bnblogs/service/NavigationService.java
index 5a9134a..94bc40d 100644
--- a/src/main/java/cc/bnblogs/service/NavigationService.java
+++ b/src/main/java/cc/bnblogs/service/NavigationService.java
@@ -2,6 +2,9 @@ package cc.bnblogs.service;
import cc.bnblogs.mapper.NavigationMapper;
import cc.bnblogs.pojo.Navigation;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@@ -12,6 +15,7 @@ import java.util.List;
* @createTime: 2022/10/19
*/
@Service
+@CacheConfig(cacheNames = {"blog-cache"})
public class NavigationService {
private final NavigationMapper navigationMapper;
@@ -49,6 +53,8 @@ public class NavigationService {
* 保存导航条
* @param navigation 待保存的导航条对象
*/
+ @CacheEvict(allEntries = true)
+
public void save(Navigation navigation) {
navigationMapper.save(navigation);
}
@@ -57,6 +63,7 @@ public class NavigationService {
* 根据id删除导航条对象
* @param id 待删除导航条的id
*/
+ @CacheEvict(allEntries = true)
public void delete(Integer id) {
navigationMapper.deleteById(id);
}
@@ -66,7 +73,17 @@ public class NavigationService {
* @param id 导航条id
* @param ordered 导航条顺序值
*/
+ @CacheEvict(allEntries = true)
public void order(Integer id, Integer ordered) {
navigationMapper.ordered(id,ordered);
}
+
+ /**
+ * 首页展示的导航条(只显示enable=true)
+ * @return 显示的导航条
+ */
+ @Cacheable
+ public List show() {
+ return navigationMapper.findAllByEnableOrderByOrderedAsc(true);
+ }
}
diff --git a/src/main/java/cc/bnblogs/service/TagService.java b/src/main/java/cc/bnblogs/service/TagService.java
index f980112..c9dbf0f 100644
--- a/src/main/java/cc/bnblogs/service/TagService.java
+++ b/src/main/java/cc/bnblogs/service/TagService.java
@@ -2,6 +2,9 @@ package cc.bnblogs.service;
import cc.bnblogs.mapper.TagMapper;
import cc.bnblogs.pojo.Tag;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -11,6 +14,7 @@ import java.util.List;
* @createTime: 2022/10/17
*/
@Service
+@CacheConfig(cacheNames = {"blog-cache"})
public class TagService {
private final TagMapper tagMapper;
@@ -23,6 +27,7 @@ public class TagService {
* 获取所有标签
* @return 标签列表
*/
+ @Cacheable
public List list() {
return tagMapper.findAll();
}
@@ -31,6 +36,7 @@ public class TagService {
* 获取标签总数
* @return 标签总数
*/
+ @Cacheable
public long count() {
return tagMapper.count();
}
@@ -40,6 +46,7 @@ public class TagService {
* @param id 标签id
* @return id对应的标签对象
*/
+
public Tag detail(Integer id) {
return tagMapper.findById(id).orElse(null);
}
@@ -48,6 +55,8 @@ public class TagService {
* 保存标签
* @param tag 待保存的标签对象
*/
+ @CacheEvict(allEntries = true)
+
public void save(Tag tag) {
tagMapper.save(tag);
}
@@ -56,7 +65,18 @@ public class TagService {
* 根据id删除标签对象
* @param id 待删除标签的id
*/
+ @CacheEvict(allEntries = true)
public void delete(Integer id) {
tagMapper.deleteById(id);
}
+
+ /**
+ * 随机返回多个标签
+ * @param limit 标签数量
+ * @return 标签云
+ */
+ @Cacheable
+ public List show(int limit) {
+ return tagMapper.findRandom(limit);
+ }
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index a8451b0..86d22ff 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -30,6 +30,10 @@ spring:
servlet:
multipart:
max-file-size: 20MB
+ cache:
+ type: ehcache
+ ehcache:
+ config: classpath:/ehcache-spring.xml
logging:
file:
@@ -69,3 +73,10 @@ website:
footer: '© 2020-2022 barney 赣ICP备2022002184号-1
赣公网安备 36092202000146号'
+default-images:
+ images:
+ - /static/image/1.jpg
+ - /static/image/2.jpg
+ - /static/image/3.jpg
+ - /static/image/4.jpg
+ - /static/image/5.jpg
diff --git a/src/main/resources/ehcache-spring.xml b/src/main/resources/ehcache-spring.xml
new file mode 100644
index 0000000..d3333b0
--- /dev/null
+++ b/src/main/resources/ehcache-spring.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/templates/admin/article.html b/src/main/resources/templates/admin/article.html
index 70c34cb..f01bf43 100644
--- a/src/main/resources/templates/admin/article.html
+++ b/src/main/resources/templates/admin/article.html
@@ -112,6 +112,14 @@
return `${value.name}`;
}
},
+ {
+ title: '文章类型',
+ field: 'type',
+ align: 'center',
+ formatter: value => {
+ return value===1 ? "文章": "页面";
+ }
+ },
{
title: '浏览量',
field: 'views',
diff --git a/src/main/resources/templates/admin/common.html b/src/main/resources/templates/admin/common.html
index 8155a02..6dc22c7 100644
--- a/src/main/resources/templates/admin/common.html
+++ b/src/main/resources/templates/admin/common.html
@@ -47,7 +47,7 @@
diff --git a/src/main/resources/templates/admin/write.html b/src/main/resources/templates/admin/write.html
index 997c0b9..15ec890 100644
--- a/src/main/resources/templates/admin/write.html
+++ b/src/main/resources/templates/admin/write.html
@@ -1,7 +1,7 @@
-
+