1. 条件查询
1.1 标签查询
- POST /label/search 根据条件查询列表
(1)labelService中增加方法
/*
* 构建查询条件
*/
private Specification<Label> createSpecification(Map searchMap){
return new Specification<Label>(){
@Override public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) { List<Predicate> predicateList=new ArrayList<>(); if(searchMap.get("labelname")!=null && !"".equals(searchMap.get("labelname"))){
predicateList.add(cb.like( root.get("labelname").as(String.class), "%"+ (String)searchMap.get("labelname")+"%" ) );
}
if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){ predicateList.add(cb.equal( root.get("state").as(String.class), (String)searchMap.get("state") ) );
}
if(searchMap.get("recommend")!=null && !"".equals(searchMap.get("recommend"))){
predicateList.add(cb.equal( root.get("recommend").as(String.class), (String)searchMap.get("recommend") ) );
}
}
}
(2).LabelController
/*
* 根据条件查询
* @param searchMap
* @return
*/
@RequestMapping(value = "/search",method = RequestMethod.POST)
public Result<List> findSearch(@RequestBody Map searchMap){
return new Result<>(true,StatusCode.OK,"查询成功",labelService.findSearch(searchMap));
}
1.2 带分页的条件查询
(1)修改labelService,增加方法
/*
* 带分页的条件查询
* @Param searchMap
* @Param page
* @param size
* @return
*/
public Page<Label> findSearch(Map searchMap,int page,int size){
Specification specification = createSpecification(searchMap);
PageRequest pageRequest = PageRequest.of(page-1,size);
return labelDao.findAll(specification,pageRequest);
}
(2)修改labelController
/*
* 条件+分页查询
* @Param searchMap
* @Param page
* @Param size
* @return
*/
@RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
public Result<List> findSeach(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
Page pageList = labelService.findSearch(searchMap,page,size);
return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
}
2.问答微服务开发
2.1 表结构分析
问题表
回答表
问答标签中间表
2.2 实体类
@Entity
@Table(name = "tb_pl")
public class Pl implements Serializable{
@Id
private String problemid;
@Id
private String labelid;
//setters/getters...
}
2.3 Dao层增加方法
/*
*根据标签ID查询最新问题列表
*@Param labelId
*@Param pageable
*@return
*/
@Query("select p from Problem p where id in(select problemid from P1 where labelid = ?1) order by replytime desc")
public Page<Problem> findNewestListByLabelId(String labelId,Pageable pageable);
2.4 Service层增加方法
/*
* 根据标签ID查询问题列表
*@Param labelId 标签ID
*@Param page 页码
*@Param size 页大小
*@return
*/
public Page<Problem> findNewestListByLabelId(String labelId,int page,int size){
PageRequest pageRequest = PageRequest.of(page-1,size);
return problemDao.findNewListByLabelId(labelId,pageRequest);
}
2.5 ProblemController新增方法
/*
*@Param labelId 标签ID
*@return
*/
@RequestMapping(value="/newlist/{labelid}/{page}/{size}",method=RequestMe thod.GET)
public Result findNewListByLabelId(@PathVariable String labelid,@PathVariable int page,@PathVariable int size ){
Page<Problem> pageList = problemService.findNewListByLabelId(labelid, page, size);
PageResult<Problem> pageResult = new PageResult<> (pageList.getTotalElements(), pageList.getContent());
return new Result(true, StatusCode.OK, "查询成功",pageResult);
}
3. 缓存处理
为了提高查询的性能,我们通常采用Redis缓存解决。
3.1 SpringDataRedis
Spring-Data-Redis是spring大家族的一部分,提供了在spring应用中通过简单的配置访问redis服务,对redis底层开发包进行了高度封装,RedisTemplate
3.2 配置
(1)在pom.xml中引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐data‐redis</artifactId> </dependency>
(2)修改application.yml,在spring节点下添加配置
redis:
host:192.168.184.134
(3)修改Service层,引入RedisTemplate,并修改方法
@Autowired
private RedisTemplate redisTemplate;
/*
* 根据Id查询实体
*/
public Article findById(String id){
//从缓存中提取
Article article = (Article)redisTemplate.opsForValue().get("article_"+id);
//如果缓存中没有,则到数据库查询并放入缓存
if(article == null){
article = articleDao.findById(id).get();
redisTemplate.opsForValue().set("article_" + id,article);
}
return article;
}
(4)修改或者删除后清空缓存
/*
* 修改
* @param article
*/
public void update(Article article){
redisTemplate.delete("article_" + article.getId());
articleDao.save(article);
}
/*
* 删除
* @Param id
*/
public void deleteById(String id){
redisTemplate.delete("article" + id); //删除缓存
articleDao.deleteById(id);
}
3.3 缓存过期处理
修改findById方法,设置1天的过期时间
redisTemplate.opsForValue().set("article_" + id,article,1,TimeUnit.DAYS);