一个简易的搜索功能解决方案

This commit is contained in:
ronger 2020-11-30 23:56:34 +08:00
parent 047697ecd9
commit a8c27e29fd
6 changed files with 131 additions and 4 deletions

View File

@ -0,0 +1,17 @@
package com.rymcu.forest.dto;
import lombok.Data;
/**
* @author ronger
*/
@Data
public class SearchModel {
private String label;
private String value;
private String type;
}

View File

@ -0,0 +1,28 @@
package com.rymcu.forest.mapper;
import com.rymcu.forest.dto.SearchModel;
import java.util.List;
/**
* @author ronger
*/
public interface SearchMapper {
/**
* 初始化文章搜索数据
* @return
*/
List<SearchModel> searchInitialArticleSearch();
/**
* 初始化作品集搜索数据
* @return
*/
List<SearchModel> searchInitialPortfolioSearch();
/**
* 初始化用户搜索数据
* @return
*/
List<SearchModel> searchInitialUserSearch();
}

View File

@ -0,0 +1,16 @@
package com.rymcu.forest.service;
import com.rymcu.forest.dto.SearchModel;
import java.util.List;
/**
* @author ronger
*/
public interface SearchService {
/**
* 初始化搜索数据
* @return
*/
List<SearchModel> initialSearch();
}

View File

@ -0,0 +1,43 @@
package com.rymcu.forest.service.impl;
import com.rymcu.forest.core.service.redis.RedisResult;
import com.rymcu.forest.core.service.redis.RedisService;
import com.rymcu.forest.dto.SearchModel;
import com.rymcu.forest.mapper.SearchMapper;
import com.rymcu.forest.service.SearchService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @author ronger
*/
@Service
public class SearchServiceImpl implements SearchService {
@Resource
private SearchMapper searchMapper;
@Resource
private RedisService redisService;
@Override
public List<SearchModel> initialSearch() {
String searchKey = "initialSearch";
RedisResult<SearchModel> result = redisService.getListResult(searchKey, SearchModel.class);
if (Objects.nonNull(result.getListResult())) {
return result.getListResult();
}
List<SearchModel> list = new ArrayList<>();
List<SearchModel> articleSearchModels = searchMapper.searchInitialArticleSearch();
List<SearchModel> portfolioSearchModels = searchMapper.searchInitialPortfolioSearch();
List<SearchModel> userSearchModels = searchMapper.searchInitialUserSearch();
list.addAll(articleSearchModels);
list.addAll(portfolioSearchModels);
list.addAll(userSearchModels);
redisService.set(searchKey, list, 24 * 60 * 60);
return list;
}
}

View File

@ -8,10 +8,7 @@ import com.rymcu.forest.core.result.GlobalResultMessage;
import com.rymcu.forest.core.service.log.annotation.VisitLogger; import com.rymcu.forest.core.service.log.annotation.VisitLogger;
import com.rymcu.forest.dto.*; import com.rymcu.forest.dto.*;
import com.rymcu.forest.entity.User; import com.rymcu.forest.entity.User;
import com.rymcu.forest.service.ArticleService; import com.rymcu.forest.service.*;
import com.rymcu.forest.service.JavaMailService;
import com.rymcu.forest.service.PortfolioService;
import com.rymcu.forest.service.UserService;
import com.rymcu.forest.util.UserUtils; import com.rymcu.forest.util.UserUtils;
import com.rymcu.forest.util.Utils; import com.rymcu.forest.util.Utils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -37,6 +34,8 @@ public class CommonApiController {
private ArticleService articleService; private ArticleService articleService;
@Resource @Resource
private PortfolioService portfolioService; private PortfolioService portfolioService;
@Resource
private SearchService SearchService;
@GetMapping("/get-email-code") @GetMapping("/get-email-code")
public GlobalResult<Map<String, String>> getEmailCode(@RequestParam("email") String email) throws MessagingException { public GlobalResult<Map<String, String>> getEmailCode(@RequestParam("email") String email) throws MessagingException {
@ -135,4 +134,10 @@ public class CommonApiController {
Map map = Utils.getArticlesGlobalResult(pageInfo); Map map = Utils.getArticlesGlobalResult(pageInfo);
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(map);
} }
@GetMapping("/initial-search")
public GlobalResult initialSearch() {
List<SearchModel> list = SearchService.initialSearch();
return GlobalResultGenerator.genSuccessResult(list);
}
} }

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.rymcu.forest.mapper.SearchMapper">
<resultMap id="BaseResultMap" type="com.rymcu.forest.dto.SearchModel">
<result column="label" property="label"></result>
<result column="value" property="value"></result>
<result column="type" property="type"></result>
</resultMap>
<select id="searchInitialArticleSearch" resultMap="BaseResultMap">
select article_title as label, id as value, 'article' as type from vertical_article where article_status = 0
</select>
<select id="searchInitialPortfolioSearch" resultMap="BaseResultMap">
select portfolio_title as label, id as value, 'portfolio' as type from vertical_portfolio
</select>
<select id="searchInitialUserSearch" resultMap="BaseResultMap">
select nickname as label, nickname as value, 'user' as type from vertical_user where status = 0
</select>
</mapper>