✨ 发布文章界面加载标签
This commit is contained in:
parent
4cd5f99898
commit
98d3ee1ce8
17
src/main/java/com/rymcu/vertical/dto/LabelModel.java
Normal file
17
src/main/java/com/rymcu/vertical/dto/LabelModel.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.rymcu.vertical.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author ronger
|
||||
*/
|
||||
@Data
|
||||
public class LabelModel implements Serializable {
|
||||
|
||||
private String label;
|
||||
|
||||
private String value;
|
||||
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
package com.rymcu.vertical.mapper;
|
||||
|
||||
import com.rymcu.vertical.core.mapper.Mapper;
|
||||
import com.rymcu.vertical.dto.LabelModel;
|
||||
import com.rymcu.vertical.entity.Tag;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TagMapper extends Mapper<Tag> {
|
||||
Integer insertTagArticle(@Param("idTag") Integer idTag, @Param("idArticle") Integer idArticle);
|
||||
|
||||
@ -16,4 +19,9 @@ public interface TagMapper extends Mapper<Tag> {
|
||||
Integer deleteUnusedTag();
|
||||
|
||||
Integer update(@Param("idTag") Integer idTag, @Param("tagUri") String tagUri, @Param("tagIconPath") String tagIconPath, @Param("tagStatus") String tagStatus, @Param("tagDescription") String tagDescription, @Param("tagReservation") String tagReservation);
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
List<LabelModel> selectTagLabels();
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package com.rymcu.vertical.service;
|
||||
|
||||
import com.rymcu.vertical.core.service.Service;
|
||||
import com.rymcu.vertical.dto.LabelModel;
|
||||
import com.rymcu.vertical.entity.Article;
|
||||
import com.rymcu.vertical.entity.Tag;
|
||||
import com.rymcu.vertical.web.api.exception.BaseApiException;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -34,4 +36,10 @@ public interface TagService extends Service<Tag> {
|
||||
* @return
|
||||
*/
|
||||
Map saveTag(Tag tag);
|
||||
|
||||
/**
|
||||
* 获取标签列表
|
||||
* @return
|
||||
*/
|
||||
List<LabelModel> findTagLabels();
|
||||
}
|
||||
|
@ -1,13 +1,16 @@
|
||||
package com.rymcu.vertical.service.impl;
|
||||
|
||||
import com.rymcu.vertical.core.service.AbstractService;
|
||||
import com.rymcu.vertical.core.service.redis.RedisService;
|
||||
import com.rymcu.vertical.dto.ArticleTagDTO;
|
||||
import com.rymcu.vertical.dto.LabelModel;
|
||||
import com.rymcu.vertical.entity.Article;
|
||||
import com.rymcu.vertical.entity.Tag;
|
||||
import com.rymcu.vertical.entity.User;
|
||||
import com.rymcu.vertical.mapper.ArticleMapper;
|
||||
import com.rymcu.vertical.mapper.TagMapper;
|
||||
import com.rymcu.vertical.service.TagService;
|
||||
import com.rymcu.vertical.util.CacheUtils;
|
||||
import com.rymcu.vertical.util.UserUtils;
|
||||
import com.rymcu.vertical.web.api.exception.BaseApiException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@ -33,6 +36,8 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
|
||||
private TagMapper tagMapper;
|
||||
@Resource
|
||||
private ArticleMapper articleMapper;
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class })
|
||||
@ -139,4 +144,14 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LabelModel> findTagLabels() {
|
||||
List<LabelModel> list = (List<LabelModel>) CacheUtils.get("tags");
|
||||
if (list == null) {
|
||||
list = tagMapper.selectTagLabels();
|
||||
CacheUtils.put("tags", list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
139
src/main/java/com/rymcu/vertical/util/CacheUtils.java
Normal file
139
src/main/java/com/rymcu/vertical/util/CacheUtils.java
Normal file
@ -0,0 +1,139 @@
|
||||
package com.rymcu.vertical.util;
|
||||
|
||||
import org.apache.shiro.cache.Cache;
|
||||
import org.apache.shiro.cache.CacheManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Cache工具类
|
||||
*/
|
||||
public class CacheUtils {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
|
||||
private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);
|
||||
|
||||
private static final String SYS_CACHE = "system";
|
||||
|
||||
/**
|
||||
* 获取SYS_CACHE缓存
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Object get(String key) {
|
||||
return get(SYS_CACHE, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SYS_CACHE缓存
|
||||
* @param key
|
||||
* @param defaultValue
|
||||
* @return
|
||||
*/
|
||||
public static Object get(String key, Object defaultValue) {
|
||||
Object value = get(key);
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入SYS_CACHE缓存
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static void put(String key, Object value) {
|
||||
put(SYS_CACHE, key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从SYS_CACHE缓存中移除
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static void remove(String key) {
|
||||
remove(SYS_CACHE, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Object get(String cacheName, String key) {
|
||||
return getCache(cacheName).get(getKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param defaultValue
|
||||
* @return
|
||||
*/
|
||||
public static Object get(String cacheName, String key, Object defaultValue) {
|
||||
Object value = get(cacheName, getKey(key));
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public static void put(String cacheName, String key, Object value) {
|
||||
getCache(cacheName).put(getKey(key), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中移除
|
||||
* @param cacheName
|
||||
* @param key
|
||||
*/
|
||||
public static void remove(String cacheName, String key) {
|
||||
getCache(cacheName).remove(getKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中移除所有
|
||||
* @param cacheName
|
||||
*/
|
||||
public static void removeAll(String cacheName) {
|
||||
Cache<String, Object> cache = getCache(cacheName);
|
||||
Set<String> keys = cache.keys();
|
||||
for (Iterator<String> it = keys.iterator(); it.hasNext();){
|
||||
cache.remove(it.next());
|
||||
}
|
||||
logger.info("清理缓存: {} => {}", cacheName, keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存键名,多数据源下增加数据源名称前缀
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
private static String getKey(String key){
|
||||
// String dsName = DataSourceHolder.getDataSourceName();
|
||||
// if (StringUtils.isNotBlank(dsName)){
|
||||
// return dsName + "_" + key;
|
||||
// }
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个Cache,没有则显示日志。
|
||||
* @param cacheName
|
||||
* @return
|
||||
*/
|
||||
private static Cache<String, Object> getCache(String cacheName){
|
||||
Cache<String, Object> cache = cacheManager.getCache(cacheName);
|
||||
if (cache == null){
|
||||
throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。");
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
@ -5,7 +5,10 @@ import com.github.pagehelper.PageInfo;
|
||||
import com.rymcu.vertical.core.result.GlobalResult;
|
||||
import com.rymcu.vertical.core.result.GlobalResultGenerator;
|
||||
import com.rymcu.vertical.dto.ArticleDTO;
|
||||
import com.rymcu.vertical.dto.LabelModel;
|
||||
import com.rymcu.vertical.entity.Tag;
|
||||
import com.rymcu.vertical.service.ArticleService;
|
||||
import com.rymcu.vertical.service.TagService;
|
||||
import com.rymcu.vertical.util.Utils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@ -22,6 +25,8 @@ public class TagController {
|
||||
|
||||
@Resource
|
||||
private ArticleService articleService;
|
||||
@Resource
|
||||
private TagService tagService;
|
||||
|
||||
@GetMapping("/{name}")
|
||||
public GlobalResult articles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows, @PathVariable String name){
|
||||
@ -31,4 +36,10 @@ public class TagController {
|
||||
Map map = Utils.getArticlesGlobalResult(pageInfo);
|
||||
return GlobalResultGenerator.genSuccessResult(map);
|
||||
}
|
||||
|
||||
@GetMapping("/tags")
|
||||
public GlobalResult tags() {
|
||||
List<LabelModel> list = tagService.findTagLabels();
|
||||
return GlobalResultGenerator.genSuccessResult(list);
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,10 @@
|
||||
<id column="created_time" property="createdTime"/>
|
||||
<id column="updated_time" property="updatedTime"/>
|
||||
</resultMap>
|
||||
<resultMap id="TagLabelResultMap" type="com.rymcu.vertical.dto.LabelModel">
|
||||
<result column="tag_title" property="label"></result>
|
||||
<result column="tag_title" property="value"></result>
|
||||
</resultMap>
|
||||
<insert id="insertTagArticle">
|
||||
insert into vertical_tag_article (id_tag,id_article,created_time,updated_time) values (#{idTag},#{idArticle},sysdate(),sysdate())
|
||||
</insert>
|
||||
@ -36,4 +40,7 @@
|
||||
<select id="selectCountUserTagById" resultType="java.lang.Integer">
|
||||
select count(*) from vertical_user_tag where id_tag = #{idTag} and id_user = #{idUser}
|
||||
</select>
|
||||
<select id="selectTagLabels" resultMap="TagLabelResultMap">
|
||||
select tag_title from vertical_tag where tag_status = 0
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user